| Previous | Table of Contents | Next |
If you do not have JDBCTest installed, you may download and install it from http://java.sun.com/products/jdbc. When JDBCTest starts, the screen in Figure 3.5 appears.
Press the Press Here To Continue button to load the main JDBCTest window. This window, shown in Figure 3.6, has a menu bar that allows you to connect to a data source, explore its metadata, and even execute SQL commands. The main window has four parts: a menu bar, a list of data source connections, result output from the driver, and a box that displays the Java code executed to perform your requests. This last box is valuable in learning JDBC because you can execute a command and then cut and paste the code into your own application. It is a quick technique for learning the library.
Follow these steps to try out JDBCTest:
Figure 3.5 Startup screen for JDBCTest.
Figure 3.6 JDBCTest main screen.
Figure 3.7 JDBCTest connection screen.
As you can see, JDBCTest offers a great deal of information about the data source that you can use to optimize and configure queries. Using this information inside your application allows you to dynamically configure data access routines. For example, knowing if the data source supports the GROUP BY clause can help you configure how to structure your queries. Lets take a look at an example for accessing and processing database metadata in your application.
Figure 3.8 JDBCTest displaying results.
Figure 3.9 JDBCTest displaying metadata.
The Connection object provides you with this information through an object implementing the DatabaseMetaData interface. Once you have a Connection object, query it for information about the structure of the underlying database by calling getMetaData(). This method returns an object implementing the DatabaseMetaData interface. (Appendix A lists the information you can retrieve about the database from the DatabaseMetaData object.)
This example creates a connection to the Inventory database and then prints a list of all the tables and their columns by requesting the database metadata from the Connection object, then requesting a list of available tables by calling the DBMetaData method getTables(). This returns a result set containing information about the tables in the database.
The method getTables takes the parameters listed in Table 3.8. This method returns a result set that consists of values returned from the database query. This result set is described later in this chapter. It contains the columns described in Table 3.9. All results in this result set are strings.
In the next example, the table names and the table types for all tables (columns 3 and 4) are printed to the console. The results are shown in Figure 3.10.
| Table 3.8 The getTables() Parameters | |
| PARAMETER | USE |
|---|---|
| String catalog | retrieves those without a catalog; null means drop catalog name from the selection criteria |
| String schemaPattern | A schema name pattern; retrieves those without a schema |
| String tableNamePattern | A table name pattern |
| String[] types | A list of table types to include; null returns all types |
| Table 3.9 The getTables() Result Set Values | |
| COLUMN NAME | VALUE |
|---|---|
| 1. TABLE_CAT | Table catalog (may be null) |
| 2. TABLE_SCHEM | Table schema (may be null) |
| 3. TABLE_NAME | Table name |
| 4. TABLE_TYPE String | Table type such as TABLE, VIEW, ALIAS |
| 5. REMARKS | Comments about the table |
| Previous | Table of Contents | Next |