loading ...
loading...

2008-09-25 | 看看老外怎么正常关闭JDBC的多个Statement

分享
标签: null  exception  老外  statement  close 

以前用JDBC处理多个Statement的时候,总会因为异常而无法完美的解决Statement正常关闭,今天无意看到老外写了以下一段代码,和大家分享一下:

Java代码
  1. private PreparedStatement psStmt1;   
  2. private PreparedStatement psStmt2;   
  3. private PreparedStatement psStmt3;   
  4.     
  5. ........................   
  6. ........................   
  7.   
  8. /*  关闭代码  */  
  9. public void cleanup() throws SQLException {   
  10.   SQLException exception = null;   
  11.   if (psStmt1 != null) {   
  12.     try {   
  13.       psStmt1.close();   
  14.     } catch (SQLException e) {   
  15.       exception = e;   
  16.     } finally {   
  17.       psStmt1 = null;   
  18.     }   
  19.   }   
  20.     
  21.   if (psStmt2 != null) {   
  22.     try {   
  23.       psStmt2.close();   
  24.     } catch (SQLException e) {   
  25.       if (exception != null) e.setNextException(exception);   
  26.       exception = e;   
  27.     } finally {   
  28.       psStmt2 = null;   
  29.     }   
  30.   }   
  31.     
  32.   if (psStmt3 != null) {   
  33.     try {   
  34.       psStmt3.close();   
  35.     } catch (SQLException e) {   
  36.       if (exception != null) e.setNextException(exception);   
  37.       exception = e;   
  38.     } finally {   
  39.       psStmt3 = null;   
  40.     }   
  41.   }   
  42.     
  43.   if (exception != null) {   
  44.     throw exception;   
  45.   }   
  46. }  
分享 分享 |  评论 (0) |  阅读 (?)  |  固定链接 |  类别 (Java) |  发表于 12:22
搜狐博客温馨提示:警惕博客留言诈骗, 搜狐博客管理员的正确地址为http://admin.blog.sohu.com, 其他都是冒牌。搜狐博客官方不会要求参加活动的各位博友缴纳任何的手续费用。请勿轻信留言、评论中的中奖信息,更不要拨打陌生电话及向陌生帐户汇款,谨防受骗!识别更多网络骗术,请 点击查看详情
您还未登录,只能匿名发表评论。或者您可以 登录 后发表。
 
  注册会员,数码相机 iPod nano等超多好礼免费拿!(倒计时:仅剩8天)
表  情:
加载中...
回复通知: 同时用小纸条通知对方该回复