Java Reference
In-Depth Information
Figure 10-2
also shows how long the application takes to get all the metadata for the display using the
sun.jdbc.odbc.JdbcOdbcDriver.
Contrast this elapsed time of nearly seven seconds with the
elapsed time of just over two seconds for the Opta2000 driver, shown in
Figure 10-3
. Although the
timing methodology used is by no means rigorous, the results speak for themselves.
Figure 10-3:
Additional DatabaseMetaData information
Many of the
DatabaseMetaData
methods take so-called "String pattern" arguments. These are
arguments that may contain a mixture of Strings and wildcards. The wild cards conform to the normal
wildcard rules for SQL Strings:
"%" means match any substring of 0 or more characters.
"_" means match any one character.
If a search-pattern argument is set to null, that argument's criteria will be ignored in the search.
Cross-
Reference
SQL escapes and wildcards are discussed in
Chapter 3
.
If a driver does not support a metadata method, a
SQLException
will normally be thrown. In the case
of methods that return a ResultSet, either a ResultSet (which may be empty) is returned or a
SQLException
is thrown.
After connecting to the SQLServerContacts database, the
DatabaseMetaData
object is first queried
for all table types, then for all tables within a type, then for columns within a table, and finally for
information about the columns themselves. The results are used to populate the
JTree
.
A
DatabaseMetaData
object is created using the
Connection.getMetaData()
method. It is then
used to get information about the database, as in the example shown in
Listing 10-4
, which gets the
types of the tables in the database.
Listing 10-4:
Retrieving table types
public Vector getTableTypes(){
Vector typeVector = new Vector();
try{
Connection con = DriverManager.getConnection(url,userName,password);
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getTableTypes();
ResultSetMetaData md = rs.getMetaData();
while(rs.next()){
typeVector.addElement(rs.getString(1));