August 24, 2022

JDBC ResultSetMetaData Interface

Using java.sql.ResultSetMetaData interface you can get metadata about the ResultSet object. When you retrieve data from DB a ResultSet object is returned with each ResultSet object there is an associated ResultSetMetaData object containing information about the returned ResultSet like table name, column count, column’s data type.

How to get ResultSetMetaData Object

You obtain the ResultSetMetaData instance by calling the getMetaData() method of the ResultSet.

ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE_NAME"); ResultSetMetaData rsmd = rs.getMetaData();

Methods in Java ResultSetMetaData

Some of the important methods of ResultSetMetaData are as given below-

  • getColumnCount()- Returns the number of columns in this ResultSet object.
  • getColumnName(int column)- Get the designated column's name.
  • getColumnType(int column)- Retrieves the designated column's SQL type.
  • getSchemaName(int column)- Get the designated column's table's schema.
  • getTableName(int column)- Gets the designated column's table name.
  • isAutoIncrement(int column)- Indicates whether the designated column is automatically numbered.

ResultSetMetaData Java Example

DB used is MySql, schema is knpcode and table used is EMPLOYEE with columns as id, FIRST_NAME, LAST_NAME and DEPARTMENT note that id is auto-incremented.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class RSMDDemo {
  public static void main(String[] args) {
    Connection connection = null;
    try {
      // Load driver
      Class.forName("com.mysql.cj.jdbc.Driver");
      // connection object
      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/knpcode", 
                   "root", "admin");
      // create Statement object   
      Statement statement = connection.createStatement();  
      ResultSet rs = statement.executeQuery("Select * from Employee");
      System.out.println("***ResultSet Metadata information***");
      ResultSetMetaData rsmd = rs.getMetaData();
      int columnCount = rsmd.getColumnCount();	      
      System.out.println("Number of columns in each row- " + columnCount);
      System.out.println("ID column auto incremented- " + rsmd.isAutoIncrement(1));
      System.out.println("Table Name- " + rsmd.getTableName(2));
      for(int i = 1; i <= columnCount; i++) {
    	  System.out.println("Column " + rsmd.getColumnName(i) + " is of type " + rsmd.getColumnTypeName(i));
      }
  //	      // Iterate through ResultSet
  //	      while(rs.next()){
  //	        System.out.println("id: " + rs.getInt("id") + 
  //	           " First Name: " + rs.getString("FIRST_NAME") + 
  //	           " Last Name: " + rs.getString("LAST_NAME")+ 
  //	           " Dept: " + rs.getString("DEPARTMENT")); 
  //	      } 
    }catch(ClassNotFoundException | SQLException e) {
      e.printStackTrace();
    }finally{
      if(connection != null){
        //close connection 
        try {
          connection.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}
Output
***ResultSet Metadata information***
Number of columns in each row- 4
ID column auto incremented- true
Table Name- employee
Column id is of type INT
Column first_name is of type VARCHAR
Column last_name is of type VARCHAR
Column department is of type VARCHAR

That's all for the topic JDBC ResultSetMetaData Interface. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment