Here's how I apply that DBConnection class.
QueryFactory is a class where I put all my SQL queries for easy-access. If someone comes in off the street and needs to update a query, it's easier for them to find if they're in a central location. You might want to create several specific factories (they aren't really factories, I just called them that), as in my case my QueryFactory has gotten a little too big to be easy to work with.
Now, be sure to notice the "finally" at the end. That tells Java to make sure to put forth every possible effort to execute what's inside that block, in this case the closing of the connection. If you didn't use the DBConnection wrapper class, all this would need to take place inside a larger "try-catch" to catch SQLExceptions, which might pre-empt the closing of the connection. By making sure that the contructor and the close method don't throw exceptions (the close method does everything possible to ensure that the connection is closed), you can put it outside the try-catch for the SQLExceptions that could occur when actually querying/processing.
Code:
public void myMethod(String arg){
String query = QueryFactory.getQuery(arg);
DBConnection conn = new DBConnection();
try{
ResultSet rset = conn.executeQuery(query);
while(rset.next()){
// do stuff
}
}catch(SQLException sqle){
System.out.println(query);
sqle.printStackTrace();
// You might choose to re-throw the exception so you can
// do higher-level error handling for the user.
}finally{
conn.close();
}
}