Skip to main content

Java ResultSetMetaData Interface, Java DatabaseMetaData interface

Java ResultSetMetaData Interface

The metadata means data about data i.e. we can get further information from the data.

If you have to get metadata of a table like total number of column, column name, column type etc. , ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object.

Commonly used methods of ResultSetMetaData interface

MethodDescription
public int getColumnCount()throws SQLExceptionit returns the total number of columns in the ResultSet object.
public String getColumnName(int index)throws SQLExceptionit returns the column name of the specified column index.
public String getColumnTypeName(int index)throws SQLExceptionit returns the column type name for the specified index.
public String getTableName(int index)throws SQLExceptionit returns the table name for the specified column index.

How to get the object of ResultSetMetaData:

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

Example of ResultSetMetaData interface :

  1. import java.sql.*;  
  2. class Rsmd{  
  3. public static void main(String args[]){  
  4. try{  
  5. Class.forName("oracle.jdbc.driver.OracleDriver");  
  6. Connection con=DriverManager.getConnection(  
  7. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  8.   
  9. PreparedStatement ps=con.prepareStatement("select * from emp");  
  10. ResultSet rs=ps.executeQuery();  
  11. ResultSetMetaData rsmd=rs.getMetaData();  
  12.   
  13. System.out.println("Total columns: "+rsmd.getColumnCount());  
  14. System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));  
  15. System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1));  
  16.   
  17. con.close();  
  18. }catch(Exception e){ System.out.println(e);}  
  19. }  
  20. }  
Output:Total columns: 2
       Column Name of 1st column: ID
       Column Type Name of 1st column: NUMBER

Java 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. DatabaseMetaData dbmd=con.getMetaData();  
  10.   
  11. System.out.println("Driver Name: "+dbmd.getDriverName());  
  12. System.out.println("Driver Version: "+dbmd.getDriverVersion());  
  13. System.out.println("UserName: "+dbmd.getUserName());  
  14. System.out.println("Database Product Name: "+dbmd.getDatabaseProductName());  
  15. System.out.println("Database Product Version: "+dbmd.getDatabaseProductVersion());  
  16.   
  17. con.close();  
  18. }catch(Exception e){ System.out.println(e);}  
  19. }  
  20. }  
Output:Driver Name: Oracle JDBC Driver
       Driver Version: 10.2.0.1.0XE
       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. }  




Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&