JDBC Database Meta Data example

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.

First we need to create DatabaseMetaData object by calling getMetaData() from Connection interface. It provide whole information about database so getMetaData() method is available in the Connection interface.

Syntax: DatabaseMetaData dmd=con.getMetaData(); // con is an object of Connection interface

The following are common methods available in DatabaseMetaData interface:

  • ResultSet getCatalogs()
  • int getDatabaseMajorVersion()
  • int getDatabaseMinorVersion()
  • String getDatabaseProductName()
  • String getDatabaseProductVersion()
  • int getDefaultTransactionIsolation()
  • int getDriverMajorVersion()
  • int getDriverMinorVersion()
  • String getDriverName()
  • String getDriverVersion()

The following source code to demostrates the DatabaseMetaData interface. It takes database name from user and display all the tables from the database.  

import java.sql.*;
import java.util.Vector;
public class DatabaseMetadataDemo1 {
Connection con;

public DatabaseMetadataDemo1() {
try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/
  test?user=root&password=root");
catch (Exception e) {
  sop("Error in connection" + e);
}
}
public void useMetaData() {
try {
  // to obtain DatabaseMetadata object
  DatabaseMetaData dm = con.getMetaData();
  sop("Get database Major Version : - "+ dm.getDatabaseMajorVersion());
  sop("Get database Minor Version : - "+ dm.getDatabaseMinorVersion());
  sop("get Database Product name : - "  + dm.getDatabaseProductName());
  sop("get Database Product Version : - "+ dm.getDatabaseProductVersion());
  sop("Get JDBC Driver Major Version :- "+ dm.getDriverMajorVersion());
  sop("Get JDBC Driver Minor Version :- "+ dm.getDriverMinorVersion());
  sop("Get driver Name : -" + dm.getDriverName());
  sop("get driver version :- " + dm.getDriverVersion());
  sop("Get JDBC Major version :- "+ dm.getJDBCMajorVersion());
  sop("Get JDBC Minor version :- "+ dm.getJDBCMinorVersion());
  sop("===========");
  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()) {
    sop(rset.getString(3));
  }
catch (Exception e) {
  e.printStackTrace();
}
}
// this method works instead of System.out.println()
public void sop(String str)
{
System.out.println(str);
}
public static void main(String[] args) {
DatabaseMetadataDemo1 obj = new DatabaseMetadataDemo1();
obj.useMetaData();
}
}
Download

Previous Next

Comment by : monika maske March 4th, 2015
Comment : this web site is awesome........................................................................!

Comment by : monika maske March 4th, 2015
Comment : this web site is very useful for student they can do us program and they can understands the programs and he will prepare the programs

Comment by : shiva April 18th, 2014
Comment : nice explanation .....

Comment by : Mukesh Shah November 24th, 2013
Comment : Superb.... example.

Comment by : Stella Dangerous October 25th, 2013
Comment : Aweeh ma se kind

Comment by : Vinayaka bc June 19th, 2013
Comment : awesome..!!! Very clearly given..!! Thanks a lot..!!


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code