In this tutorial, we are going to learn about ResultSetMetaData in JDBC. The typical definition of a MetaData is data about the data. Here ResultSetMetaData means, the data about the ResultSet is called ResultSetMetaData.

Here we listed some of the important points about ResultSetMetaData in JDBC.

ResultSetMetaData in JDBC :

  • ResultSetMetaData is an interface, which is coming from java.sql package.
  • If we want to read the data from database we need to use executeQuery() method. Obviously executeQuery() method returns ResultSet object, which consists the data rows.
  • If we want to read the data from ResultSet object, then we should have the knowledge about the data stored in ResultSet.
  • Because based on the type of data only, we need to use appropriate getXxx() method like below:
ResultSet rs = stmt.executeQuery("select * from student");
int id = rs.getInt(1);
String name = rs.getString(2);
  • If we don’t know the exact data about the ResultSet, what type of get???() method we use?
  • No problem, here is the solution. ResultSetMetaData is actually comes to help us regarding this.
  • ResultSetMetaData provides the data about the ResultSet object.
  • It will provide all necessary information about the data available in ResultSet.
  • We can get the ResultSetMetaData object by calling getMetadata() method on ResultSet object.
ResultSet rs = stmt.executeQuery("select * from student");
ResultSetMetaData resultSetMetaData = rs.getMetaData();

ResultSetMetaData in JDBC Example :

Here is the complete example for ResultSetMetaData in JDBC.

Jdbc_ResultSetMetaData_Example.java
package com.onlinetutorialspoint.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.util.Scanner;
import com.mysql.jdbc.DatabaseMetaData;
public class Jdbc_ResultSetMetaData_Example {
    public static void main(String[] args) throws Exception {
        Connection connection = null;
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlinetutorialspoint", "root", "123456");
        Statement stmt = connection.createStatement();
        String sqlQuery = "select * from student";
        ResultSet rs = stmt.executeQuery(sqlQuery);
        ResultSetMetaData resultSetMetaData = rs.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        for (int i = 1; i <= columnCount; ++i) {
            System.out.println("***************");
            System.out.print("Column Name : " + resultSetMetaData.getColumnLabel(i) + " \n");
            System.out.print("Column Type : " + resultSetMetaData.getColumnType(i) + " \n");
            System.out.print("Column Class Name : " + resultSetMetaData.getColumnClassName(i) + " \n");
            System.out.print("Column Type Name :" + resultSetMetaData.getColumnTypeName(i) + " \n");
            System.out.println("Database Name : " + resultSetMetaData.getCatalogName(i));
        }
        rs.close();
        stmt.close();
        connection.close();
    }
}

Output :

Terminal
***************
Column Name : sid
Column Type : 4
Column Class Name : java.lang.Integer
Column Type Name :INT
Database Name : onlinetutorialspoint
***************
Column Name : sname
Column Type : 12
Column Class Name : java.lang.String
Column Type Name :VARCHAR
Database Name : onlinetutorialspoint
***************
Column Name : address
Column Type : 12
Column Class Name : java.lang.String
Column Type Name :VARCHAR
Database Name : onlinetutorialspoint
***************
Column Name : age
Column Type : 4
Column Class Name : java.lang.Integer
Column Type Name :INT
Database Name : onlinetutorialspoint

Happy Learning 🙂