Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
conn = geting the connection object ( using DriverManager.getConnection() method or using connection pool)
stmt = conn.createStatement ("select ...");
// some other logic here
}
catch (SQLException e)
{
// handling the exceptions
}
finally
{
}
Here my question is while closing the connection object in the following cases what are the problems it will come.
Assume if there is no exception occurred, in try block there it self closing the connection object.
try { // same above code stmt.close(); conn.close(); } catch(Exception ex) { // handling the exceptions } finally { }
If some exception is occured so it will goes to catch block, there it self closing the connection object.
try { // same above code } catch(Exception ex) { // handling the exceptions stmt.close(); conn.close(); } finally { }
Closing the connection object in finally block.
try { // same above code } catch(Exception ex) { // handling the exceptions } finally { stmt.close(); conn.close(); }
- Difference between closing the connection object using close() method and closing the pooled connection using close().
Note: Please don't say closing the connection object in finally block is good. I know that one. If i keep the close connection in try block, catch block is there any problems please explain.