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

Example:

JDBCTest.java

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import com.w3schools.util.JDBCUtil;
 
/**
 * This class is used to show the use of ResultSetMetaData.
 * @author w3schools
 */
public class JDBCTest {
	public static void main(String args[]){
		Connection conn = null;
		Statement statement = null;
 
		String query = "select EMPLOYEE_ID, " +
				"NAME from EMPLOYEE";
 
		try{			
			//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
			statement.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

JDBCUtil.java

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

Output:

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.