首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

数据库关闭疑问,该如何解决

2012-03-21 
数据库关闭疑问ConnectionconDriverManager.getConnection(url, 123 , 123 )Statementstmtcon.crea

数据库关闭疑问
Connection   con=   DriverManager.getConnection(url, "123 ", "123 ");
              Statement   stmt   =   con.createStatement();
              ResultSet   rs=stmt.executeQuery(sql);
              假设有很长的代码(20行吧)
              再假设这20行内的代码报出SQLException   或是其它结束  
              rs.close
              stmt.close
              con.close
              上面三句都不会执行,就不能关闭连接
           

              怎么能又关了数据库所有连接   方法还向上抛出异常  
              这个方法   throws   SQLException  
              try   catch   块的话!太大了!!
           


[解决办法]
try{
tx=session.beginTransaction();
}catch(Exception ex){
throw SQLException ;}
finally{
rs.close;
stmt.close;
con.close;

}
[解决办法]
try {
// Make DB Connection
// Do whatever you need
}
catch (SQLException e) {
// Do Exception handling
// throw e; // if you have to.
}
finally {
if (DBConnection is still open)
Close it.
// Release all necessary resources.
}

热点排行