How to use JDBC ResultSetMetaData

In this tutorial you will learn about the ResultSetMetaData object of JDBC, and the name suggest this object gives you comprehensive information about the ResultSet retrieved by statement.

The interface ResultSetMetaData enables you to determine the attributes like number of columns, names, and data types for a result set.

The ResultSetMetaData interface provides descriptive information about the columns in a result set such as the number of columns it contains or each column’s data type. The interface does not provide information regarding the database or the number of rows in the result set.

You can create the ResultSetMetaData object from a valid ResultSet object.

The ResultSetMetaData interface provides you with various methods for retrieving information about the result set. You call the various setter and getter methods to retrieve data from the ResultSetMetaData object.

Below is the example which show you more about the ResultSetMetaData and how you can use it.

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 JDBCResultSetMetaDataExample {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            // Step1:Register the mysql driver
            // Step2:Get the connection object
            connection = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
            // Step3:Get the statement object
            statement = connection.createStatement();
            // Prepare the SQL query
            // Step4: Run the SQL query and get the ResultSet object.
            resultSet = statement.executeQuery(SQL);
            // Retrive the data from ResultSet
            while ( {
                System.out.println("The id of person is : " + resultSet.getString(1));
                System.out.println("The name of person is : " + resultSet.getString(2));
                System.out.println("The age of person is : " + resultSet.getString(3));
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            System.out.println("The number of columns in ResultSet is : " + resultSetMetaData.getColumnCount());
            System.out.println("The table name of column number 1 is : " + resultSetMetaData.getTableName(1));
        } catch (SQLException e) {
        } catch (ClassNotFoundException e) {
        } finally {
            try {
                if (resultSet != null) {
                if (statement != null) {
                if (connection != null) {
            } catch (SQLException e) {

The out put of the above example will be:

The id of person is : 1
The name of person is : Sam
The age of person is : 29
The id of person is : 2
The name of person is : Bob
The age of person is : 33
The id of person is : 3
The name of person is : Susan
The age of person is : 22
The number of columns in ResultSet is : 3
The table name of column number 1 is : person