ResultSetMetaData

java.sql.ResultSetMetaData Example

In this example I will discuss about ResultSetMetaData interface and its usage. It represents an object that can be used to get information about the types and properties of the columns in a ResultSet object.

The ResultSetMetaData extends the Wrapper interface, which is an interface for JDBC classes that provide the ability to retrieve the delegate instance when the instance in question is in fact a proxy class.

The ResultSetMetaData exists since JDK1.2.
 
 

The ResultSetMetaData in Java

To see how ResultSetMetaData is used, firstly create a database, a table and put some data in it by executing this SQL:

create database db_test;
create table db_test.tbl_one (
    id int not null auto_increment key,
    name varchar(20) not null,
    surname varchar(20) not null
);

insert into db_test.tbl_one values (1,'John','Doe');
insert into db_test.tbl_one values (2,'Jane','Doe');

After that, create a class called RSMDExample with this source code:

RSMDExample.java

package com.javacodegeeks.examples;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class RSMDExample {

	public static void main(String[] args) {
		String host = "jdbc:mysql://localhost/";
		String db = "db_test";
		String user = "root";
		String password = "bitnami";
		String sqlQuery = "Select * from tbl_one";
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(host+db,user,password);
			
			ResultSet rs = conn.createStatement().executeQuery(sqlQuery);
			
			ResultSetMetaData rsmd = rs.getMetaData();
			int cols = rsmd.getColumnCount();
			System.out.printf("The query fetched %d columns\n",cols);
			System.out.println("These columns are: ");
			for (int i=1;i<=cols;i++) {
				String colName = rsmd.getColumnName(i);
				String colType = rsmd.getColumnTypeName(i);
				System.out.println(colName+" of type "+colType);
				
			}
				
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}

Firstly, I am adding the necessary database drivers for MySQL database. Then, I establish a connection with the database on line 20 (make sure to change the host, username and password).

After that, I get a ResultSet instance after executing the query on line 16, which selects all the data from the table. Next, I get the ResultSetMetaData instance from the ResultSet. This instance, allows me to access some data from the table I was querying, like the number of the columns (line 25), column name (line 29), column type (line 30), and so on.

When this program is run, it gives this output:

The query fetched 3 columns
These columns are: 
id of type INT
name of type VARCHAR
surname of type VARCHAR

Download Code

Download
You can download the full source code of this example here : ResultSetMetaDataExample

Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button