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

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

locking

DBMS Locking Part I (DBMS only) TECHNICAL ARTICLES -> PERFORMANCE ARTICLES [  Back  ] [  Next  ] DBMS is often criticized for excessive locking – resulting in poor database performance when sharing data among multiple concurrent processes. Is this criticism justified, or is DBMS being unfairly blamed for application design and implementation shortfalls? To evaluate this question, we need to understand more about DBMS locking protocols. In this article, we examine how, why, what and when DBMS locks and unlocks database resources. Future articles will address how to minimize the impact of database locking. THE NEED FOR LOCKING In an ideal concurrent environment, many processes can simultaneously access data in a DBMS database, each having the appearance that they have exclusive access to the database. In practice, this environment is closely approximated by careful use of locking protocols. Locking is necessary in a concurrent environment to as...

DATA WAREHOUSE VERSUS DATA MART: THE GREAT DEBATE

DATA WAREHOUSE VERSUS DATA MART: THE GREAT DEBATE Customers exploring the field of business intelligence for the first time often lead with: What is the difference between a data warehouse and a data mart? The next question follows as predictably as night follows day: which one does my company need? Let me start by saying that the two terms are often confused. Indeed, some people in the industry use them virtually interchangeably, which is unfortunate, because they do reflect a valuable hierarchical difference. The Data Warehouse A "data warehouse" will typically contain the full range of business intelligence available to a company from all sources. That data consists of transaction-processing records, corporate and marketing data, and other business operations information; for example, a bank might include loans, credit card statements, and demand deposits data, along with basic customer information. This internal data is frequently combined with statistica...