日期:2014-05-20  浏览次数:20659 次

数据库关闭疑问
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.
}