You should close the Connection. Otherwise, the database client will typically keep the socket connection and other resources open. If you are not closing the connection, It´d not release the resources and it´d create new connection remember there is a limited number of connection only. So it may result next user not to get the database connection. Actually, the proper and safe pattern in Java is to close your ResultSet, Statement, and Connection (in that order) in a finally block when you are done with them, something like that:
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Do stuff
...
} catch (SQLException ex) {
// Exception handling stuff
...
}
finally {
try { rs.close(); } catch (Exception e) { /* ignored */ }
try { ps.close(); } catch (Exception e) { /* ignored */ }
try { conn.close(); } catch (Exception e) { /* ignored */ }
}
To improve the finally block more you should use the helper class to close the objects in null-safe helper methods and the finally block becomes something like that:
finally {
DbUtil.closeQuietly(rs);
DbUtil.closeQuietly(ps);
DbUtil.closeQuietly(conn);
}
5