«Back to Home

Core Java

Topics

DatabaseMetaData Interface In Java

DatabaseMetaData Interface
 
In JDBC, DatabaseMetaData interface provides many methods to get the meta data of a database like database product name, database product version, name of the total number of the tables and name of total number of views etc.

Methods of DatabaseMetaData interface
 
public String getDriverName()throws SQLException

This method is used to return the name of JDBC driver.
 
public String getDriverVersion()throws SQLException

This method is used to return the version number of JDBC driver.
 
public String getUserName()throws SQLException

This method is used to return the username of the database.
 
public String getDatabaseProductName()throws SQLException

This method is used to return the product name of the database.
 
public String getDatabaseProductVersion()throws SQLException

This method is used to return the product version of the database.
 
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)throws SQLException

This method is used to return the explanation of the tables of the particular catalog. The table type can be TABLE, VIEW, ALIAS, SYSTEM TABLE, SYNONYM etc.
 
Get the DatabaseMetaData object

The getMetaData() method of the connection interface is used to return the DatabaseMetaData object.
 
Syntax

public DatabaseMetaData getMetaData()throws SQLException
 
Let’s see an example1, given below.
 
Code
  1. import java.sql.*;  
  2. public class StudentDatabase2 {  
  3.     public static void main(String args[]) throws Exception {  
  4.         Class.forName("org.apache.derby.jdbc.ClientDriver");  
  5.         String url = "jdbc:derby://localhost:1527/Student";  
  6.         String username = "Student";  
  7.         String password = "student";  
  8.         Connection conn = DriverManager.getConnection(url, username, password);  
  9.         DatabaseMetaData d = conn.getMetaData();  
  10.         System.out.println("Driver Name: " + d.getDriverName());  
  11.         System.out.println("Driver Version: " + d.getDriverVersion());  
  12.         System.out.println("UserName: " + d.getUserName());  
  13.         System.out.println("Database Product Name: " + d.getDatabaseProductName());  
  14.         System.out.println("Database Product Version: " + d.getDatabaseProductVersion());  
  15.         conn.close();  
  16.     }  
  17. }  
  18.    
24

Output

25

Let’s see an example2, which prints total number of tables, given below.
 
Code
  1. import java.sql.*;  
  2. public class StudentDatabase2 {  
  3.     public static void main(String args[]) throws Exception {  
  4.         Class.forName("org.apache.derby.jdbc.ClientDriver");  
  5.         String url = "jdbc:derby://localhost:1527/Student";  
  6.         String username = "Student";  
  7.         String password = "student";  
  8.         Connection conn = DriverManager.getConnection(url, username, password);  
  9.         DatabaseMetaData d = conn.getMetaData();  
  10.         String table[] = {"TABLE"};  
  11.         ResultSet rs = d.getTables(nullnullnull, table);  
  12.         while (rs.next()) {  
  13.             System.out.println(rs.getString(3));  
  14.         }  
  15.         conn.close();  
  16.     }  
  17. }  
26
 
Output

27

Let’s see an example3, given below, which prints total number of views.
 
Code
  1. import java.sql.*;  
  2. public class StudentDatabase2 {  
  3.     public static void main(String args[]) throws Exception {  
  4.         Class.forName("org.apache.derby.jdbc.ClientDriver");  
  5.         String url = "jdbc:derby://localhost:1527/Student";  
  6.         String username = "Student";  
  7.         String password = "student";  
  8.         Connection conn = DriverManager.getConnection(url, username, password);  
  9.         DatabaseMetaData d = conn.getMetaData();  
  10.         String table[] = {"VIEW"};  
  11.         ResultSet rs = d.getTables(nullnullnull, table);  
  12.         while (rs.next()) {  
  13.             System.out.println(rs.getString(3));  
  14.         }  
  15.         conn.close();  
  16.     }  
  17. }  
28

Output

29

In the above example, we can see that program don’t display anything because there is no view in the table of database.
 
Summary

Thus, we learnt, JDBC DatabaseMetaData interface provides many methods to get meta data of a database and also learnt its important methods in Java.