Contents | Prev | Next JDBCTM Guide: Getting Started

Appendix C: Implementation notes

C.1     ResultSet lookups by method name

Here is some specimen code that implements the ResultSet.findColumn and (for example) ResultSet.getString using ResultSetMetaData.

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)));

C.2     Object finalization

Applets are advised to call "close" on the various JDBC objects such as Statement, ResultSet, Connection, when they are done with them. However, some applets will forget and some applets may get killed before they can close these objects.

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.

Contents | Prev | Next or
Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved.