Java Reference
In-Depth Information
}
}
Once the program has run, the database is populated and ready for use by the UserDBJDBC
data accessor shown in Using JDBC Prepared Statements .
Finding JDBC Metadata
Problem
You want to learn about a database or table.
Solution
Read the documentation provided by your vendor or database administrator. Or ask the soft-
ware for a MetaData object.
Discussion
There are two classes relating to metadata (data about data) that you can ask for in the JDBC
API: DatabaseMetaData and ResultSetMetaData . Each of these has methods that let you
interrogate particular aspects. The former class is obtained from a get method in a Connec-
tion object; the latter from a get method in the given ResultSet .
ResultSetMetaData
First, let's look at the class ResultsDecoratorHTML , a “generic query” formatter shown in
Example 18-10 . This is one of several “ResultSet Formatters” used in the SQLRunner pro-
gram of Program: SQLRunner (the parent class ResultsDecorator , discussed with SQLRun-
ner , simply defines a Constructor that saves the given PrintWriter as a field, as well as
providing two abstract methods that ResultsDecoratorHTML implements). When a program
using ResultsDecoratorHTML calls the write() method, the ResultSet is interrogated and
formatted into a neat little HTML table, using the column names from the Res-
ultSetMetaData as the headings for the HTML table. The nice part about this program is
that it responds to whatever columns are in the ResultSet , which need not be in the same
order as they are in the database. Consider the two queries:
Search WWH ::




Custom Search