AdSense

AdSense3

Tuesday, 13 January 2015

DatabaseMetaData interface

DatabaseMetaData interface provides methods to get meta data of a database such as database product name, database product version, driver name, name of total number of tables, name of total number of views etc.

Commonly used methods of DatabaseMetaData interface

  • public String getDriverName()throws SQLException: it returns the name of the JDBC driver.
  • public String getDriverVersion()throws SQLException: it returns the version number of the JDBC driver.
  • public String getUserName()throws SQLException: it returns the username of the database.
  • public String getDatabaseProductName()throws SQLException: it returns the product name of the database.
  • public String getDatabaseProductVersion()throws SQLException: it returns the product version of the database.
  • public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)throws SQLException: it returns the description of the tables of the specified catalog. The table type can be TABLE, VIEW, ALIAS, SYSTEM TABLE, SYNONYM etc.

How to get the object of DatabaseMetaData:

The getMetaData() method of Connection interface returns the object of DatabaseMetaData. Syntax:
  1. public DatabaseMetaData getMetaData()throws SQLException  

Simple Example of DatabaseMetaData interface :

  1. import java.sql.*;  
  2. class Dbmd{  
  3. public static void main(String args[]){  
  4. try{  
  5. Class.forName("oracle.jdbc.driver.OracleDriver");  
  6.   
  7. Connection con=DriverManager.getConnection(  
  8. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  9.   
  10. DatabaseMetaData dbmd=con.getMetaData();  
  11.   
  12. System.out.println("Driver Name: "+dbmd.getDriverName());  
  13. System.out.print("Driver Version: "+dbmd.getDriverVersion());  
  14. System.out.println("\tUserName: "+dbmd.getUserName());  
  15. System.out.println("Database Product Name: "+dbmd.getDatabaseProductName());  
  16. System.out.println("Database Product Version: "+dbmd.getDatabaseProductVersion());  
  17.   
  18. con.close();  
  19.   
  20. }catch(Exception e){ System.out.println(e);}  
  21.   
  22. }  
  23. }  
Output:Driver Name: Oracle JDBC Driver
       Driver Version: 10.2.0.1.0XE     UserName: Kundan
       Database Product Name: Oracle
       Database Product Version: Oracle Database 10g Express Edition
                                 Release 10.2.0.1.0 -Production

Example of DatabaseMetaData interface that prints total number of tables :

  1. import java.sql.*;  
  2. class Dbmd2{  
  3. public static void main(String args[]){  
  4. try{  
  5. Class.forName("oracle.jdbc.driver.OracleDriver");  
  6.   
  7. Connection con=DriverManager.getConnection(  
  8. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  9.   
  10. DatabaseMetaData dbmd=con.getMetaData();  
  11. String table[]={"TABLE"};  
  12. ResultSet rs=dbmd.getTables(null,null,null,table);  
  13.   
  14. while(rs.next()){  
  15. System.out.println(rs.getString(3));  
  16. }  
  17.   
  18. con.close();  
  19.   
  20. }catch(Exception e){ System.out.println(e);}  
  21.   
  22. }  
  23. }  

Example of DatabaseMetaData interface that prints total number of views :

  1. import java.sql.*;  
  2. class Dbmd3{  
  3. public static void main(String args[]){  
  4. try{  
  5. Class.forName("oracle.jdbc.driver.OracleDriver");  
  6.   
  7. Connection con=DriverManager.getConnection(  
  8. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  9.   
  10. DatabaseMetaData dbmd=con.getMetaData();  
  11. String table[]={"VIEW"};  
  12. ResultSet rs=dbmd.getTables(null,null,null,table);  
  13.   
  14. while(rs.next()){  
  15. System.out.println(rs.getString(3));  
  16. }  
  17.   
  18. con.close();  
  19.   
  20. }catch(Exception e){ System.out.println(e);}  
  21.   
  22. }  
  23. }  

No comments:

Post a Comment