Lesson 2 | Another MetaData class |
Objective | Describe 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:
getXXX()
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:
- getColumnCount() - This method returns the number of columns in the result set.
- getColumnLabel(int column) - This method returns the label that describes the specified column in the result set.
- getColumnType(int column) - This method returns the SQL type code for the specified column in the result set.
- getColumnName(int column) - This method returns the name of the specified column in the result set.
- getColumnDisplaySize(int column) - This method returns the display size of the specified column in the result set.
- isNullable(int column) - This method returns the nullability of the specified column in the result set.
- getPrecision(int column) - This method returns the precision of the specified column in the result set.
- getScale(int column) - This method returns the scale of the specified column in the result set.
- 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.