JDBC  «Prev  Next»
Lesson 2Another MetaData class
ObjectiveDescribe what the ResultSetMetaData class offers

ResultSetMetaData Class (JDBC)

If you used Oracle's Javadocs as a reference for any of the classes and methods discussed previously, you may have noticed that there is a ResultSetMetaData class. It is similar to the DatabaseMetaData class in that it can provide some information about the object it references. In this case, it can tell you about the ResultSet you are using. Use the methods in this class if you need to know the names of a table's columns or the data types of those columns, for example.

Method forms

There are slightly more than 20 methods in the class, and they take one of two forms:
  1. getXXX()
  2. isXXX()

property name

Here XXX is a property name where you are trying to get the corresponding value for a column, or you are testing to determine if a column has a certain property. All methods are invoked on an instance of a ResultSetMetaData object, which is created from an existing ResultSet. Below is an example of creating that object and then using it to get information about that ResultSet.

ResultSet rs = stmt.executeQuery(
"SELECT hospital, rating from Hospitals");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
boolean b = rsmd.isSearchable(1);

This example creates a ResultSetMetaData object and then gets the number of columns in the ResultSet and tests to determine if the ResultSet is searchable. The full set of options is documented in the SDK docs for the java.sql.ResultSetMetaData interface and can be accessed from the Resources section using the course toolbar.

Commonly used JDBC Methods within ResultSetMetaData class

The most commonly used methods within the ResultSetMetaData class in JDBC are:
  1. getColumnCount() - This method returns the number of columns in the result set.
  2. getColumnLabel(int column) - This method returns the label that describes the specified column in the result set.
  3. getColumnType(int column) - This method returns the SQL type code for the specified column in the result set.
  4. getColumnName(int column) - This method returns the name of the specified column in the result set.
  5. getColumnDisplaySize(int column) - This method returns the display size of the specified column in the result set.
  6. isNullable(int column) - This method returns the nullability of the specified column in the result set.
  7. getPrecision(int column) - This method returns the precision of the specified column in the result set.
  8. getScale(int column) - This method returns the scale of the specified column in the result set.
  9. getTableName(int column) - This method returns the name of the table from which the specified column was selected.

These methods allow the developer to retrieve information about the columns in the result set, such as their names, types, and sizes, which can be used for a variety of purposes such as formatting output, validating data, or creating new queries based on the existing data.

In the next lesson, you will consider some issues that relate to the Brazil Hospital Project.