JDBC MetaData

MetaData

MetaData is the additional information about the data what we store in database applications. We can think of this as data about data. There are mainly two type of meta data available in the database systems in context of JDBC API.

  1. DatabaseMetaData
  2. ResultSetMetaData

JDBC API provides interfaces to fetch different type of meta data information. It's responsibility of database ventor to provide implementing classes to these interfaces.

1. DatabaseMetaData Interface

  • Provides comprehensive information about the database
  • This interface is implemented by drivers vendors
  • This helps user to know the capabilities of DBMS in combination with the driver based on JDBC technology that is used with it
  • Different RDBMSs often support different features, implement features in different ways, and use different data types.
  • In addition, a driver may implement a feature on top of what the DBMS offers
  • Information returned by methods in this interface applies to the capabilities of a particular driver and a particular DBMS working together
  • Why and when this information is needed
    • A user for this interface is commonly a tool that needs to discover how to deal with the underlying DBMS.
    • This is especially true for applications that are intended to be used with more than one DBMS.
    • For example, a tool might use the method getTypeInfo to find out what data types can be used in a CREATE TABLE statement.
    • Or a user might call the method supportsCorrelatedSubqueries to see if it is possible to use a correlated subquery
    • Or supportsBatchUpdates to see if it is possible to use batch updates
  • Some DatabaseMetaData methods return lists of information in the form of ResultSet objects.
  • Regular ResultSet methods, such as getString and getInt, can be used to retrieve the data from these ResultSet objects.
  • If a given form of metadata is not available, an empty ResultSet will be returned.
  • Additional columns beyond the columns defined to be returned by the ResultSet object for a given method can be defined by the JDBC driver vendor and must be accessed by their column label.
  • Some DatabaseMetaData methods take arguments that are String patterns.
  • These arguments all have names such as fooPattern.
  • Within a pattern String, "%" means match any substring of 0 or more characters, and "_" means match any one character.
  • Only metadata entries matching the search pattern are returned. If a search pattern argument is set to null, that argument's criterion will be dropped from the search.

Commonly used methods of DatabaseMetaData interface

Modifier, Type Method and Description

  • String getDatabaseProductName() - Retrieves the name of this database product.
  • String getDatabaseProductVersion() - Retrieves the version number of this database product.
  • String getDriverName() - Retrieves the name of this JDBC driver.
  • String getDriverVersion() - Retrieves the version number of this JDBC driver as a String.
  • String getURL() - Retrieves the URL for this DBMS.
  • String getUserName() - Retrieves the user name as known to this database.
See complete program on DatabaseMetaData here

2. ResulSetMetaData Interface

  • An object that can be used to get information about the types and properties of the columns in a ResultSet object.
  • The following code fragment
    • creates the ResultSet object rs,
    • creates the ResultSetMetaData object rsmd, and
    • uses rsmd to find out how many columns rs has and whether the first column in rs can be used in a WHERE clause.
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
boolean b = rsmd.isSearchable(1);

Commonly used methods of ResultSetMetaData interface

Modifier, Type Method and Description

  • String getSchemaName(int column) - Get the designated column's table's schema.
  • String getTableName(int column) - Gets the designated column's table name.
  • String getColumnName(int column) - Get the designated column's name.
  • int getColumnCount() - Returns the number of columns in this ResultSet object.
  • String getColumnTypeName(int column) - Retrieves the designated column's database-specific type name.
Footer with Map