JDBC Metadata

The simple meaning of metadata is data about data. There are two metadata available in the JDBC API - ResultSetMetaData and DatabaseMetaData.

                                          ResultSetMetaData
It is used to make descriptive information about ResultSetobject, like; number of columns, name of columns and dataype of columns. It does not provide any information regarding database and how many rows are available in the ResultSet object. Whether ResultSet is read only, updatable or scrollable.
This is useful when you don't have any information about the columns of the table.

First we have to create object of ResultSetMetadata by calling getMetaData() method from ResultSet object. 
Syntax : 
             ResultSetMetaData rsmd=res.getMetaData(); 

The following are common methods in ResultSetMetadata interface -
  • int getColumnCount()
  • String getColumnName()
  • int getColumnType()
  • String getTableName()
The following is the source code to demostrates ResultSetMetaData interface. Here we will display the contents of Locations table along with column heading:
PROGRAM:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

class JDBCMetaData
{
public static void main( String args[] )
{
Statement stmt=null;
Connection conn=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
// Get a statement from the connection
stmt = conn.createStatement() ;
// Execute the query
ResultSet rs = stmt.executeQuery( "SELECT * FROM Locations" ) ;
// Get the metadata
ResultSetMetaData md = rs.getMetaData() ;
// Print the column labels
for( int i = 1; i <= md.getColumnCount(); i++ )
System.out.print( md.getColumnLabel(i) + " " ) ;
System.out.println() ;
// Loop through the result set
while( rs.next() )
{
for( int i = 1; i <= md.getColumnCount(); i++ )
System.out.print( rs.getString(i) + " " ) ;
System.out.println() ;
}
// Close the result set, statement and the connection
rs.close() ;
stmt.close() ;
conn.close() ;
}
catch( SQLException se )
{
System.out.println( "SQL Exception:" ) ;
// Loop through the SQL Exceptions
while( se != null )
{
System.out.println( "State  : " + se.getSQLState()  ) ;
System.out.println( "Message: " + se.getMessage()   ) ;
System.out.println( "Error  : " + se.getErrorCode() ) ;
se = se.getNextException() ;
}
}
catch( Exception e )
{
System.out.println( e ) ;
}
}
}


                                          DatabaseMetaData
The java.sql.DatabaseMetaData interface provide comprehensive information about the database. It contains the information about Database Management System (DBMS) and all the objects in the database, like; all the tables, cataloge name, view, stored procedure etc. 

Through the java.sql.DatabaseMetaData interface you can obtain meta data about the database you have connected to. For instance, you can see what tables are defined in the database, and what columns each table has, whether given features are supported etc.

PROGRAM:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class JDBCDatabaseMetaData {
Connection con;

public JDBCDatabaseMetaData() {
try {
// Load the database driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Get a connection to the database
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
} catch (Exception e) {
System.out.println("Error in connection" + e);
}
}

public void useMetaData() {
try {
// to obtain DatabaseMetadata object
DatabaseMetaData dm = con.getMetaData();
System.out.println("Get database Major Version : - "+ dm.getDatabaseMajorVersion());
System.out.println("Get database Minor Version : - "+ dm.getDatabaseMinorVersion());
System.out.println("get Database Product name : - " + dm.getDatabaseProductName());
System.out.println("get Database Product Version : - "+ dm.getDatabaseProductVersion());
System.out.println("Get JDBC Driver Major Version :- "+ dm.getDriverMajorVersion());
System.out.println("Get JDBC Driver Minor Version :- "+ dm.getDriverMinorVersion());
System.out.println("Get driver Name : -" + dm.getDriverName());
System.out.println("get driver version :- " + dm.getDriverVersion());
System.out.println("Get JDBC Major version :- "+ dm.getJDBCMajorVersion());
System.out.println("Get JDBC Minor version :- "+ dm.getJDBCMinorVersion());
System.out.println("===========");
String[] types = { "TABLE" };
String catalog = con.getCatalog();
String schema = "test"; // name of the database
DatabaseMetaData dmd = con.getMetaData();
ResultSet rset = dmd.getTables(catalog, schema, null, types);
while (rset.next()) {
System.out.println(rset.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
JDBCDatabaseMetaData obj = new JDBCDatabaseMetaData();
obj.useMetaData();
}
}

NOTE:For more methods of ResultSetMetaData and DatabaseMetaData refer javadocs


Comments

Popular posts from this blog

Servlet Advantages and Disadvantages

The Deployment Descriptor: web.xml

Session Management