JDBC DatabaseMetaData interface

The metadata refers to the data about data. The DatabaseMetaData interface provides the facility to get the information like driver name, total number of tables and driver version etc. We can get the object of DatabaseMetaData by calling getMetaData() method of Connection interface.
Syntax: DatabaseMetaData dbmd=conn.getMetaData();

Commonly used methods of DatabaseMetaData:

1. getDriverName(): It is used to get thename of the JDBC driver.
Syntax: public String getDriverName()throws SQLException
2. getDriverVersion(): It is used to get the JDBC driver version.
Syntax: public String getDriverVersion()throws SQLException
3. getUserName(): It is used to get the database username.
Syntax: public String getUserName()throws SQLException
4. getDatabaseProductName(): It is used to get the database product name.
Syntax: public String getDatabaseProductName()throws SQLException
5. getDatabaseProductVersion(): It is used to get the database product version.
Syntax: public String getDatabaseProductVersion()throws SQLException
6. getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types): It is used to get the tables detail of the specified catalog.
Syntax: public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)throws SQLException


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import com.javawithease.util.JDBCUtil;
* This class is used to show the use of DataBaseMetaData.
* @author javawithease

public class JDBCTest {
public static void main(String args[]){
Connection conn = null;
//get connection
conn = JDBCUtil.getConnection();
//get DatabaseMetaData
DatabaseMetaData dbmd = conn.getMetaData();
//Result set meta data
System.out.println("Driver Name: "
+ dbmd.getDriverName());
System.out.println("Driver Version: "
+ dbmd.getDriverVersion());
System.out.println("DB UserName: "
+ dbmd.getUserName());
System.out.println("DB Product Name: "
+ dbmd.getDatabaseProductName());
System.out.println("DB Product Version: "
+ dbmd.getDatabaseProductVersion());
//close connection
}catch(Exception e){
import java.sql.Connection;
import java.sql.DriverManager;
* This is a utility class for JDBC connection.
* @author jawithease

public class JDBCUtil {
//JDBC and database properties.
private static final String DB_DRIVER =
private static final String DB_URL =
private static final String DB_USERNAME = "system";
private static final String DB_PASSWORD = "oracle";
public static Connection getConnection(){
Connection conn = null;
//Register the JDBC driver
//Open the connection
conn = DriverManager.
if(conn != null){
System.out.println("Successfully connected.");
System.out.println("Failed to connect.");
}catch(Exception e){
return conn;


Successfully connected.
Driver Name: Oracle JDBC driver
Driver Version:
DB Product Name: Oracle
DB Product Version:
Oracle Database 10g Express Edition
Release - Production

No comments: