Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   2. JDBC - database access  >   2.8. Metadata

2.8. Metadata
Last updated: 1 January 2013.

This tutorial will give you tips about metadata in JDBC. All the classes and interfaces used in the code samples of this page belong to the package java.sql.

Metadata refers to information about the driver and the database used by an application. For instance, the metadata holds the driver's name, version, supported functionalities and so forth. It also holds the database maximum number of allowed concurrent connections, the names of the tables' columns and so on. The interfaces DatabaseMetaData and ResultSetMetaData provide a rich set of methods that all JDBC drivers must implement.

For example, at startup, you may want to log information about the driver and database used by your application. The following sample uses a DatabaseMetaData object obtained from a Connection object to print to the standard output the driver's version and the database product version:

DatabaseMetaData metaData = connection.getMetaData();
System.out.println
("Driver version : " + metaData.getDriverVersion());
System.out.println
("DB product version : " + metaData.getDatabaseProductVersion());

To take another example, you may lack information about the column names of a table. If you want to display the table's columns in a GUI, you can get their names as shown in the sample below:

// Create a database connection
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "yourName", "yourPassword");

// Create a PreparedStatement
PreparedStatement statement = connection.prepareStatement("SELECT * FROM MYSTERIOUS_TABLE");

// Execute the SQL query and get the ResultSet
ResultSet resultSet = statement.executeQuery();

// Get the column count
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();

// Walk through the column names
for(int i=1; i<=columnCount; ++i){
   
String columnName = metaData.getColumnName(i);
   
// do something with columnName
    // ...
}


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   2. JDBC - database access  >   2.8. Metadata
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7