the best way is to close the db connection in the finally block:
try{
Connection connection ...
}catch(Exception e){
e.printStacktrace();
}finally{
if(connection!=null)
connection.close();
}catch(Exception ee){
ee.printStacktrace();
}