Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 [个人原创]通用的JDBC的try…catch…finally模板
jerryjerry123





发贴: 58
积分: 12
于 2005-05-12 10:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在日常的开发中, 我发现JDBC操作的Exception处理方法可总结为下面模板, 希望对大家有用.
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs=null;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");

// Get connection from DriverManager or from DataSource
conn = DriverManager.getConnection("connection string", "username", "pwd");

stmt = conn.prepareStatement("SELECT count( * ) FROM user_objects order by object_name");
rs = stmt.executeQuery();
if (rs.next())
{
……
……
}
rs.close();

} catch (Exception e)
{
System.out.println("[Exception] - " + e.toString());
} finally {
try {
if (stmt != null)
stmt.close();

if (conn != null)
conn.close();
} catch (Exception fe)
{
System.out.println("[Exception] - " + fe.toString());
}
}


jerryjerry123 edited on 2005-05-12 10:10

作者 Re:[个人原创]通用的JDBC的try…catch…finally模板 [Re:jerryjerry123]
lqefn





发贴: 1
积分: 0
于 2005-06-03 13:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
没有什么用处。


作者 Re:[个人原创]通用的JDBC的try…catch…finally模板 [Re:lqefn]
YuLimin

简单就是美

版主


发贴: 866
积分: 123
于 2005-06-04 09:07 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
if (rs.next())
{
……
……
}

一般是
while(rs.next())
{
……
……
}



 当兵不后悔!后悔不当兵! 
超越黎明时空,追逐时代潮流!
人,是要靠自己的!简单就是美!
我的Java:http://www.Java2Class.net
我的Blog:http://YuLimin.ItEye.com
作者 Re:[个人原创]通用的JDBC的try…catch…finally模板 [Re:jerryjerry123]
anit





发贴: 8
积分: 0
于 2005-06-22 14:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
未免太简单了点嘛~


作者 Re:[个人原创]通用的JDBC的try…catch…finally模板 [Re:YuLimin]
serena





发贴: 4
积分: 0
于 2005-07-07 10:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
YuLimin wrote:
if (rs.next())
{
……
……
}

一般是
while(rs.next())
{
……
……
}


佩服,我都没有看的这样仔细



作者 Re:[个人原创]通用的JDBC的try…catch…finally模板 [Re:jerryjerry123]
diystyle





发贴: 1
积分: 0
于 2005-07-14 08:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
太老了吧


作者 Re:[个人原创]通用的JDBC的try…catch…finally模板 [Re:jerryjerry123]
newbiemen





发贴: 103
积分: 20
于 2005-07-20 10:25 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

query = "SELECT COUNTStart FROM cms_users";
    stmt.execute(query);
    rs = stmt.getResultSet();
    if (rs.next()) {
      int count = rs.getInt(1);
      System.out.println("count = " + count);
    }
    query = "SELECT USR_ID, USR_EMAIL, USR_GLOB_SUB FROM cms_users ORDER BY usr_id";
    rs = stmt.executeQuery(query);
//    rs = stmt.getResultSet();
    while (rs.next()) {
      double usrId = rs.getDouble(1);
      String usrEmail = new String(rs.getString(2));
      int usrGlobSub = rs.getInt(3);
      System.out.println("usrId = " + usrId + " usrEmail = " + usrEmail + " usrGlobSub = " + usrGlobSub);
    }
    rs.close();
    stmt.close();
    con.close();



作者 Re:[个人原创]通用的JDBC的try…catch…finally模板 [Re:jerryjerry123]
wsfx





发贴: 12
积分: 0
于 2005-09-08 11:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
早就用了,不过应该感谢楼主的热心!Blush


作者 Re:[个人原创]通用的JDBC的try…catch…finally模板 [Re:jerryjerry123]
wdmsyf





发贴: 1
积分: 0
于 2005-11-19 08:27 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这是有问题滴,当在执行rs.close之前 ,如果出现Excetion,那样会导致rs不能关闭而导致内存泄漏



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923