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:
- 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).
- while(r != null): Knowing nothing about JDBC it should be obvious that r could never change from not null to null.
- while(r.next()) is the standard way to use JDBC.
- 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.)
- The result should always be closed in a finally block.
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);
}