My thoughts as an enterprise Java developer.

Wednesday, March 30, 2005

How not to use JDBC

This bad example came from production code. :-) (DatabaseManager is a wrapper for JDBC and behaves similarly.)

String select = "select ...";
Result r = null;
List numberList = new ArrayList();
try {
r = DatabaseManager.ExecuteQuery(select);
while (r != null) {
r.next();
numberList.add(r.getString("col"));
}
}
catch (Exception e) {
Result.Close(r);//Closes the result while handling null and any IOException.
}

A few of the problems with this code:
  1. DatabaseManager.ExecuteQuery can never return null (but I can let that slide some since a programmer would have to look at the code to know that -- or look at the other 64 examples in code to see that no other code checks for a null return value).
  2. while(r != null): Knowing nothing about JDBC it should be obvious that r could never change from not null to null.
  3. while(r.next()) is the standard way to use JDBC.
  4. This code uses an Exception to get out of the while loop! (Note that I have done something like that once but there was at least a somewhat good reason and it was documented.)
  5. The result should always be closed in a finally block.
So here is the improved code:

String select = "select ...";
Result r = null;
List trackingNumberList = new ArrayList();
try {
r = DatabaseManager.ExecuteQuery(select);
while (r.next()) {
trackingNumberList.add(r.getString("col"));
}
} catch (Exception e) {
e.printStackTrace();
//Logging would be nice but a stack trace to standard error is better than nothing.
} finally {
Result.Close(r);
}

No comments: