ResultSetMetaData is an interface in java.sql used to retrieve structural information about a ResultSet. It allows developers to understand the database table format dynamically without prior knowledge of its schema.
- Provides details like number of columns and their properties
- Useful for dynamic query handling and generic applications
- Accessed using getMetaData() method from a ResultSet.
Obtaining ResultSetMetaData
To get metadata from a ResultSet:
ResultSet rs = statement.executeQuery("SELECT * FROM employees");
ResultSetMetaData rsmd = rs.getMetaData();
Here:
- rs is the ResultSet from a SQL query.
- rs.getMetaData() returns a ResultSetMetaData object.
Steps to use ResultSetMetaData in JDBC
Below are the steps to use the RSMD (ResultSetMetaData) in JDBC to retrieve information about ResultSet Columns.
Step 1: Import the package
Import the required interface
import java.sql.ResultSetMetaData;
Step 2: Establish database connection
Connect Java application to database
Connection con = DriverManager.getConnection(url, user, password);
Step 3: Create Statement object
it is used to execute SQL queries
Statement stmt = con.createStatement();
Step 4: Execute query and get ResultSet
Run SELECT query and store result
ResultSet rs = stmt.executeQuery("SELECT * FROM table");
Step 5: Create ResultSetMetaData object
Retrieve metadata from ResultSet
ResultSetMetaData rsmd = rs.getMetaData();
Step 6: Use ResultSetMetaData methods
Access column details (count, name, type, etc.)
int count = rsmd.getColumnCount();
String name = rsmd.getColumnName(1);
String type = rsmd.getColumnTypeName(1);
getMetaData:The getMetaData() method of ResultSet is used to obtain a ResultSetMetaData object that describes the structure of the data returned by the query. It provides information about columns without modifying the actual data.
- It is called on a ResultSet object and returns metadata of that same result
- The returned object can be used to access details like column count and properties using methods such as getColumnCount() and getColumnName()

Implementation of RSMD Methods
Before we go on with the Java program, We should have set up our database beforehand set up with the required JDBC connectors up and working.
- First, we must create the ResultSetMetaData object.
- After creation of the object, we are ready to retrieve column set information.
we took the sample database we are going to extract metadata of this database.

some of the Common important methods of RSMD.
1. getColumnName(int columnNumber)
Returns the name of the specified column in the ResultSet
Note: Column index starts from 1, and invalid index → runtime exception.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
class GFG {
public static void main (String[] args) {
try {Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "username", "password");
Statement stmt = con.createStatement(); //creating object of statement interface
String query = "SELECT * FROM testtable";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rstmd=rs.getMetaData(); //creating an object of Resultsetmetadata Interface
System.out.println("1st Column name :"+rstmd.getColumnName(1)); //name of 1st column
System.out.println("2nd Column name :"+rstmd.getColumnName(2)); //name of 2nd column
System.out.println("4th Column name :"+rstmd.getColumnName(4)); //Runtime exception as column 4 does not exists
}catch(Exception e){e.printStackTrace();}
}
}
Output:

Explanation:This program connects to a database, executes a query, and uses ResultSetMetaData to retrieve column names from the result. It also demonstrates that accessing a non-existing column index causes a runtime exception.
2. getColumnCount()
This method, returns an integer value of total column count in the ResultSet.
import java.sql.*;
class GFG
{
public static void main (String[] args)
{
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "username", "password");
Statement stmt = con.createStatement(); // creating object of statement interface
String query = "SELECT * FROM testtable";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rstmd = rs.getMetaData(); // creating an object of Resultsetmetadata Interface
int columnsNumber = rstmd.getColumnCount(); // getting the number of column in table
System.out.println("Column Number: " + columnsNumber);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:

Explanation:This program executes a query on testtable and uses ResultSetMetaData to get and print the total number of columns using getColumnCount().
3. getColumnTypeName(int ColumnNumber)
Returns the data type of the specified column as a String; throws an exception if the column index is invalid.
import java.sql.*;
// Driver Class
class GFG {
// Main Function
public static void main(String[] args)
{
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test",
"username","password");
// Creating object of statement interface
Statement stmt = con.createStatement();
String query = "SELECT * FROM testtable";
ResultSet rs = stmt.executeQuery(query);
// Creating an object of Resultsetmetadata
// Interface
ResultSetMetaData rstmd = rs.getMetaData();
System.out.println("Type of column 1 :" + rstmd.getColumnTypeName(1));
System.out.println("Type of column 2 :" + rstmd.getColumnTypeName(2));
System.out.println("Type of column 3 :" + rstmd.getColumnTypeName(3));
// Gives type of first column, can
// Be used for data validation
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:

Explanation:This program executes a query on testtable and uses ResultSetMetaData to print the data type of each column using getColumnTypeName(), which is useful for data validation.
4. getPrecision(int columnNumber)
Returns the size or precision of the specified column as an integer; useful for handling input length and data validation.
Note: The value returned by getPrecision() depends on the column type: for numeric → max digits, for character -> length in characters, for date/time → string length, and 0 if size cannot be determined
import java.sql.*;
class GFG
{
// Main Function
public static void main (String[] args)
{
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test",
"username", "password");
// Creating Object of Statement Interface
Statement stmt = con.createStatement();
String query = "SELECT * FROM testtable";
ResultSet rs = stmt.executeQuery(query);
// Creating an Object of Resultsetmetadata Interface
ResultSetMetaData rstmd = rs.getMetaData();
// Returns Precision of Int
System.out.println("Precesion : " + rstmd.getPrecision(1));
// Returns Precision of String
System.out.println("Precesion : " + rstmd.getPrecision(2));
// Returns Precision of Int
System.out.println("Precesion : " + rstmd.getPrecision(3));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:

Explanation:This program retrieves data from testtable and uses ResultSetMetaData.getPrecision() to determine the size or precision of each column. It helps handle variable-length data and validate input dynamically, with the returned value depending on the column type (numeric, character, or date/time).
Some Other Methods Of RSMD
Below is the list of some frequently used method in RSMD:
Method Name | Return Type |
|
|---|---|---|
getColumnCount() | int | returns the number of columns in the ResultSet object. |
isNullable(int column) | int | returns 0(no null values allowed) ,1(null allowed),2(unknown nullability). |
getColumnDisplaySize(int column) | int | returns column's maximum width in characters. |
getScale(int column) | int | returns the number of digits after decimal point in the column ,0 if not applicable. |
getPrecision(int column) | int | returns the designated column length. |
getColumnLabel(int column) | String | returns the alias specified for the column, if no alias specified returns the column name. |
getColumnName(int column) | String | returns the specified column name. |
getSchemaName(int column) | String | returns the schema for the designated column's table. |
getTableName(int column) | String | returns the name of the table. |
getColumnTypeName(int column) | String | returns the column's datatype name. |
isAutoIncrement(int column) | boolean | returns if the column automatically increments. |
isWritable(int column) | boolean | returns true if the column is writable, false otherwise. |
isReadOnly(int column) | boolean | returns true if column is not writable, false otherwise. |