Metadata in Java Database Programming

In the preceding sections, you saw how to populate, query, and update database tables. However, JDBC can give you additional information about the structure of a database and its tables. For example, you can get a list of the tables in a particular database or the column names and types of a table. This information is not useful when you are implementing a business application with a predefined database. After all, if you design the tables, you know their structure. Structural information is, however, extremely useful for programmers who write tools that work with any database.

In SQL, data that describe the database or one of its parts are called metadata (to distinguish them from the actual data stored in the database). You can get three kinds of metadata: about a database, about a result set, and about parameters of prepared statements.

To find out more about the database, request an object of type DatabaseMetaData from the database connection.

DatabaseMetaData meta = conn.getMetaData();

Now you are ready to get some metadata. For example, the call

ResuttSet mrs = meta.getTables(null, null, null, new String[] { “TABLE” });

returns a result set that contains information about all tables in the database. (See the API note at the end of this section for other parameters to this method.)

Each row in the result set contains information about a table in the database. The third column is the name of the table. (Again, see the API note for the other columns.) The following loop gathers all table names:

while (


There is a second important use for database metadata. Databases are complex, and the SQL standard leaves plenty of room for variability. Well over a hun­dred methods in the DatabaseMetaData interface can inquire about the database, including calls with such exotic names as




Clearly, these are geared toward advanced users with special needs—in par­ticular, those who need to write highly portable code that works with multiple databases.

The DatabaseMetaData interface gives data about the database. A second metadata interface, ResuttSetMetaData, reports information about a result set. Whenever you have a result set from a query, you can inquire about the number of columns and each column’s name, type, and field width. Here is a typical loop:

ResuttSet rs = stat.executeQuery(“SELECT * FROM ” + tabteName);

ResuttSetMetaData meta = rs.getMetaData();

for (int i = 1; i <= meta.getColumnCount(); i++)


String cotumnName = meta.getColumnLabel(i);

int cotumnWidth = meta.getColumnDisplaySize(i);


In this section, we will show you how to write such a simple tool. The program in Listing 5.4 uses metadata to let you browse all tables in a database. The program also illustrates the use of a cached row set.

The combo box on top displays all tables in the database. Select one of them, and the center of the frame is filled with the field names of that table and the values of the first row, as shown in Figure 5.6. Click Next and Previous to scroll through the rows in the table. You can also delete a row and edit the row values. Click the Save button to save the changes to the database.

Source: Horstmann Cay S. (2019), Core Java. Volume II – Advanced Features, Pearson; 11th edition.

Leave a Reply

Your email address will not be published. Required fields are marked *