如果我有以下代码,这是否是关闭 Connection、ResultSet 和 Statement 对象的正确方法?我觉得所有的调用都close()
应该在 finally 块中。
Connection con = null;
ResultSet rs = null;
Statement stmt = null;
try{
//Code before the while loop
con = DriveManager.getConnection("Stuff");
while(someBoolean){
stmt = con.createStatement();
rs = stmt.executeQuery("SQL query");
// do stuff with query results.
if( rs != null){
rs.close();
}
if( stmt != null){
stmt.close();
}
} //end while
if( con != null ){
con.close();
}
catch (Exception e){
//handle exception
}