Java Reference
In-Depth Information
centerPanel.add(featuresPanel,BorderLayout.CENTER);
getContentPane().add(centerPanel,BorderLayout.CENTER);
}
}
Clearly, this example illustrates only a small percentage of the data available through the use of the
DatabaseMetaData
object. It is well worth referring to the Javadocs available on the Sun Web site at:
http://java.sun.com/j2se/1.4/docs/api/java/sql/package-summary.html
.
Note
A shorter link is
http://java.sun.com/docs/
. This takes you to he main Javadocs
page, and you can navigate from there.
In addition to
DatabaseMetaData
methods, JDBC provides a large number of useful methods for
accessing information about the
ResultSet
returned by a query. The
next section
discusses these
methods.
Using ResultSetMetaData
The
ResultSetMetaData
object is similar to the
DatabaseMetaData
object, with the exception that
it returns information specific to the columns in a
ResultSet
.
Information about the columns in a ResultSet is available by calling the
getMetaData()
method on
the ResultSet. The
ResultSetMetaData
object returned gives the number, types, and properties of its
ResultSet
object's columns.
Table 10-3
shows some of the more commonly used methods of the
ResultSetMetaData
object.
Table 10-3: ResultSetMetaData Methods
ResultSetMetaData method
Description
getColumnCount()
Returns the number of columns in the ResultSet
getColumnDisplaySize(int column)
Returns the column's max width in chars
getColumnLabel(int column)
Returns the column title for use in displays
getColumnName(int column)
Returns the column name
getColumnType(int column)
Returns the column's SQL data-type index
getColumnTypeName(int column)
Returns the name of the column's SQL data type
getPrecision(int column)
Returns the number of decimal digits in the column
getScale(int column)
Returns the number of digits to the right of the
decimal point
getTableName(int column)
Returns the table name
isAutoIncrement(int column)
Returns true if the column is autonumbered
isCurrency(int column)
Returns true if the column value is a currency value
isNullable(int column)
Returns true if the column value can be set to NULL
Listing 10-9
illustrates the use of the
ResultSetMetaData
methods
getColumnCount
and
getColumnLabel
in an example where the column names and column count are unknown.
Listing 10-9: Using ResultSetMetaData
public void printResultSet(String query){