VOOZH about

URL: https://www.geeksforgeeks.org/java/jdbc-resultsetmetadata-for-resultset-column-examination/

⇱ JDBC ResultSetMetaData for ResultSet Column Examination - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JDBC ResultSetMetaData for ResultSet Column Examination

Last Updated : 6 Apr, 2026

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()

👁 Steps to Create ResultSetMetaData Object

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.

👁 Sample Database Example

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.

Output:

👁 String Name of Column

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.

Output:

👁 Column Count

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.

Output:

👁 Datatype of the designated column

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

Output:

👁 Integer of the specified column size

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


Description

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.

Comment
Article Tags: