Contents | Prev | Next | JDBCTM Guide: Getting Started |
private java.util.Hashtable s2c; // Maps strings to column indexes
private ResultSetMetaData md; // Our metadata object.
public synchronized int findColumn(String columnName)
throws SQLException {
// Make a mapping cache if we don't already have one.
if (md == null) {
md = getMetaData();
s2c = new java.util.Hashtable();
}
// Look for the mapping in our cache.
Integer x = (Integer)s2c.get(columnName);
if (x != null) {
return (x.intValue());
}
// OK, we'll have to use metadata.
for (int i = 1; i < md.getColumnCount(); i++) {
if (md.getColumnName(i).equalsIgnoreCase(columnName)) {
// Success! Add an entry to the cache.
s2c.put(columnName, new Integer(i));
return (i);
}
}
throw new SQLException("Column name not found", "S0022");
}
// now the individual get-by-column-name methods are easy:
public String getString(String columnName) throws SQLException {
return (getString(findColumn(columnName)));
}
If JDBC drivers have state associated with JDBC objects that needs to get explicitly cleared up, then they should take care to provide "finalize" methods. The garbage collector will call these finalize methods when the objects are found to be garbage, and this will give the driver a chance to close (or otherwise clean up) the objects. Note, however, that there is no guarantee that the garbage collector will ever run, so you can't rely on the finalizers being called.