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:

1
2
3
4
5
6
7
8
9
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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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:

1
2
3
4
5
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
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

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