Pick a Free OS

User login


An Introduction to JDBC


stmt.executeUpdate( query );

Database Metadata:

JDBC can be used to obtain information about the structure of a

database and its tables. For example, you can get a list of tables in

a particular database and the column names for any table. This

information is useful when programming for any database. The structure

of a database may not be known to the programmer, but can be obtained

by using metadata statements. In SQL, the statements used to describe

the database and its parts are known as metadata statements.

Two types of metadata can be retrieved with JDBC. The first type

describes the database and the second type describes a result set. The

DatabaseMetaData class contains over a hundred methods to inquire

about the database. Some of the methods are exotic. A common method is

the getTables method.

DatabaseMetaData dmd = con.getMetaData();

ResultSet rs = dmd.getTables( null, null, null, new String[]

{"TABLE"} );

The parameters passed to getTables in order are a catalog (group of

related schemas), a schema (group of related tables) pattern, a table

name pattern, and a type array. Some of the types include table, view,

and system table. If null is passed, then no pattern is used to limit

the metadata information retrieved. Some of the other methods include

getDataProductVersion() , getTablePrivileges() , and getDriverName() .

The result set rs, returned contains information about all the tables

in the database. Each row contains information about a table. For

example, the third column of any row of the result set is the table

name string.

Useful metadata can be obtained about a result set after the execution

of a query. When a result set is obtained after the execution of a

query, the metadata statements can be used to extract information such

as the number of columns, column types and width.

ResultSet rs = stmt.executeQuery("Select * from test_table");

ResultSetMetaData rsmd = rs.getMetaData();

The rsmd.getColumnCount() method returns the number of columns in the

test_table and the rsmd.getColumnLabel(i) method returns the name of

the ith column. Similarly, the rsmd.getColumnDisplaySize(i) method

returns the width of the ith column. There are a number of other

methods described in the JDBC API which can be used to extract all