Monday , November 20 2017
Home / jdbc / ResultSetMetaData in JDBC Example

ResultSetMetaData in JDBC Example

In this tutorial, we are going to learn about ResultSetMetaData in JDBC. The typical definition of a MetaData is data about the data. Here ResultSetMetaData means, the data about the ResultSet is called ResultSetMetaData.

Here we listed some of the important points about ResultSetMetaData in JDBC.

ResultSetMetaData in JDBC :

  • ResultSetMetaData is an interface, which is coming from java.sql package.
  • If we want to read the data from database we need to use executeQuery() method. Obviously executeQuery() method returns ResultSet object, which consists the data rows.
  • If we want to read the data from ResultSet object, then we should have the knowledge about the data stored in ResultSet.
  • Because based on the type of data only, we need to use appropriate getXxx() method like below:

ResultSet rs = stmt.executeQuery(“select * from student”);

int id = rs.getInt(1);

String name = rs.getString(2);

  • If we don’t know the exact data about the ResultSet, what type of get???() method we use?
  • No problem, here is the solution. ResultSetMetaData is actually comes to help us regarding this.
  • ResultSetMetaData provides the data about the ResultSet object.
  • It will provide all necessary information about the data available in ResultSet.
  • We can get the ResultSetMetaData object by calling getMetadata() method on ResultSet object.

ResultSet rs = stmt.executeQuery(“select * from student”);

ResultSetMetaData resultSetMetaData = rs.getMetaData();

ResultSetMetaData in JDBC Example :

Here is the complete example for ResultSetMetaData in JDBC.


package com.onlinetutorialspoint.jdbc; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.*; 
import java.util.Scanner; 
 
import com.mysql.jdbc.DatabaseMetaData; 
 
public class Jdbc_ResultSetMetaData_Example { 
 
    public static void main(String[] args) throws Exception { 

        Connection connection = null; 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        connection = DriverManager.getConnection( 
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root", 
                "123456"); 
        Statement stmt = connection.createStatement(); 
        String sqlQuery = "select * from student"; 
        ResultSet rs = stmt.executeQuery(sqlQuery); 
        ResultSetMetaData resultSetMetaData = rs.getMetaData(); 
        int columnCount = resultSetMetaData.getColumnCount(); 
        for (int i = 1; i <= columnCount; ++i) { 
            System.out.println("***************"); 
            System.out.print("Column Name : "+resultSetMetaData.getColumnLabel(i) + " \n"); 
            System.out.print("Column Type : "+resultSetMetaData.getColumnType(i) + " \n"); 
            System.out.print("Column Class Name : "+resultSetMetaData.getColumnClassName(i) 
                    + " \n"); 
            System.out.print("Column Type Name :"+resultSetMetaData.getColumnTypeName(i) 
                    + " \n"); 
            System.out.println("Database Name : "+resultSetMetaData.getCatalogName(i)); 
             
        } 
        rs.close(); 
        stmt.close(); 
        connection.close(); 
    } 
}

Output :

***************
Column Name : sid
Column Type : 4
Column Class Name : java.lang.Integer
Column Type Name :INT
Database Name : onlinetutorialspoint
***************
Column Name : sname
Column Type : 12
Column Class Name : java.lang.String
Column Type Name :VARCHAR
Database Name : onlinetutorialspoint
***************
Column Name : address
Column Type : 12
Column Class Name : java.lang.String
Column Type Name :VARCHAR
Database Name : onlinetutorialspoint
***************
Column Name : age
Column Type : 4
Column Class Name : java.lang.Integer
Column Type Name :INT
Database Name : onlinetutorialspoint

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

JDBC Updatable ResultSet Example

Whenever we create a ResultSet object which never allows us to update the database through …

Leave a Reply

Your email address will not be published. Required fields are marked *