java tutorial - Java resultsetmetadata | Resultsetmetadata - java programming - learn java - java basics - java for beginners



Java ResultSetMetaData Interface

  • java.sql.ResultSetMetaData is also one of the frequently used interface in the JDBC API.
  • This interface provides quick overview about a ResultSet object like number of columns, column name, data type of a column etc.
  • You often need this info about a ResultSet object before processing the actual data of a ResultSet.

ResultSetMetaData In JDBC

  • ResultSetMetaData is an interface in java.sql package of JDBC API which is used to get the metadata about a ResultSet object.
  • Whenever you query the database using SELECT statement, the result will be stored in a ResultSet object.
  • Every ResultSet object is associated with one ResultSetMetaData object.
  • This object will have all the meta data about a ResultSet object like schema name, table name, number of columns, column name, datatype of a column etc.
  • You can get this ResultSetMetaData object using getMetaData() method of ResultSet.

Methods of ResultSetMetaData interface

MethodDescription
public int getColumnCount()throws
SQLException
Returns the total number of columns in ResultSet object.
public String getColumnName(int index)throws
SQLException
Returns the column name of specified column index.
public String getColumnTypeName(int index)throws
SQLException
Returns the column type name for the specified index.
public String getTableName(int index)throws
SQLException
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

public ResultSetMetaData getMetaData()throws SQLException  
click below button to copy the code. By - java tutorial - team

Sample Code

Create Table “wikitechy_employee” :

CREATE TABLE  "wikitechy_employee"
(    "ID" NUMBER NOT NULL ENABLE, 
    "FIRST_NAME" VARCHAR2(200), 
    "LAST_NAME" VARCHAR2(200), 
    "DISIGNATION" VARCHAR2(200)
);
click below button to copy the code. By - java tutorial - team

Java Program

import java.sql.*;
  
public class ResultSetMetaDataExample
{
    static
    {
        //Registering The Driver Class
  
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        }
        catch (ClassNotFoundException e)
        {
            System.out.println("Unable To Load The Driver class");
        }
    }
  
    public static void main(String[] args)
    {
        Connection con = null;
  
        Statement stmt = null;
         
        ResultSet rs = null;
  
        try
        {
            //Database Credentials
              
            String URL = "jdbc:oracle:thin:@localhost:1521:XE";
  
            String username = "username";
  
            String password = "password";
  
            //Creating The Connection Object
  
            con = DriverManager.getConnection(URL, username, password);
  
            //Creating The Statement Object
  
            stmt = con.createStatement();
  
            //Constructing The SQL Query
  
            String sql = "SELECT * FROM wikitechy_employee";
  
            //Executing The Query
  
            rs = stmt.executeQuery(sql);
             
            //getting ResultSetMetaData object
             
            ResultSetMetaData wiki = rs.getMetaData();
                     
            //getting number of columns in 'rs'
             
            int colCount = wiki.getColumnCount();
             
            System.out.println("Number Of Columns : "+colCount);
             
            System.out.println("column Details :");
             
            for (int i = 1; i <= colCount; i++)
            {
                //getting column name of index 'i'
                 
                String colName = wiki.getColumnName(i);
                 
                //getting column's data type of index 'i'
                 
                String colType = wiki.getColumnTypeName(i);
                 
                System.out.println(colName+" is of type "+colType);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            //Closing The DB Resources
  
            //Closing the ResultSet object
             
            try
            {
                if(rs!=null)
                {
                    rs.close();
                    rs=null;
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
             
            //Closing the Statement object
            try
            {
                if(stmt!=null)
                {
                    stmt.close();
                    stmt=null;
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
  
            //Closing the Connection object
  
            try
            {
                if(con!=null)
                {
                    con.close();
                    con=null;
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }
}
click below button to copy the code. By - java tutorial - team

Output

Number Of Columns : 4
column Details :
ID is of type NUMBER
FIRST_NAME is of type VARCHAR2
LAST_NAME is of type VARCHAR2
DISIGNATION is of type VARCHAR2

Related Searches to Java resultsetmetadata | Resultsetmetadata