Easy learning with example program codes

JDBC ResultSetMetaData interface

The metadata refers to the data about data. The ResultSetMetaData interface provides the facility to get the information like table name, total number of column, column name and column type etc. We can get the object of ResultSetMetaData by calling getMetaData() method of ResultSet interface.

Syntax: ResultSetMetaData rsmd = resultSet.getMetaData();  

Commonly used methods of ResultSetMetaData:

1. getTableName(int index): It returns the name of the table of the specified column index.

Syntax: public String getTableName(int index) throws SQLException

2. getColumnCount(): It returns the no. of columns in the result set.

Syntax: public int getColumnCount() throws SQLException

3. getColumnName(int index): It returns the name of the column at the specified column index.

Syntax: public String getColumnName(int index) throws SQLException

4. getColumnTypeName(int index): It returns the type of the column at the specified column index.

Syntax: public String getColumnTypeName(int index) throws SQLException


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import com.codesjava.util.JDBCUtil;
 * This class is used to show the use of ResultSetMetaData.
 * @author codesjava
public class JDBCTest {
	public static void main(String args[]){
		Connection conn = null;
		Statement statement = null;
		String query = "select EMPLOYEE_ID, " +
				"NAME from EMPLOYEE";
			//get connection
			conn = JDBCUtil.getConnection();
			//create statement
			statement = conn.createStatement();
			//execute query
			ResultSet rs = statement.executeQuery(query);
			//get ResultSetMetaData
			ResultSetMetaData rsmd = rs.getMetaData();  
			//Result set meta data
			System.out.println("Total cols: " 
					+ rsmd.getColumnCount());
			System.out.println("Column name: " 
					+ rsmd.getColumnName(1));
			System.out.println("Column type: " 
				    + rsmd.getColumnTypeName(1));
			//close connection
		}catch(Exception e){

import java.sql.Connection;
import java.sql.DriverManager;
 * This is a utility class for JDBC connection.
 * @author codesjava
public class JDBCUtil {
	//JDBC and database properties.
	private static final String DB_DRIVER = 
	private static final String DB_URL = 
	private static final String DB_USERNAME = "system";
	private static final String DB_PASSWORD = "oracle";
	public static Connection getConnection(){
		Connection conn = null;
			//Register the JDBC driver
			//Open the connection
			conn = DriverManager.
			if(conn != null){
			   System.out.println("Successfully connected.");
			   System.out.println("Failed to connect.");
		}catch(Exception e){
		return conn;


Successfully connected.
Total cols: 2
Column name: EMPLOYEE_ID
Column type: NUMBER

Download this example.
Next Topic: JDBC DatabaseMetaData interface.
Previous Topic: JDBC retrieve image example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Posted in JDBC   

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki