Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Java Programming » JDBC » ResultSetMetaData interface in JDBC
Suryateja Pericherla Categories: JDBC. No Comments on ResultSetMetaData interface in JDBC
0
(0)

The ResultSetMetaData interface in JDBC can be used to gather meta data about the result set. The meta data can include information like number of columns in a result set, type of a column, name of a column, table name, etc.
 

Some of the methods available in ResultSetMetaData interface are given below:

Method Description
public int getColumnCount() throws SQLException Returns the number of columns in the result set
public String getColumnName(int index) throws SQLException Returns the column name of the given index
public String getColumnTypeName(int index) throws SQLException Returns the column data type of the given index
public String getTableName(int index )throws SQLException Returns the table name for the given index

 

To get the required meta data of a result set we will use the getMetaData() method as shown in the below example JDBC program:

import java.sql.*;
public class GetData {
   static final String DB_URL = "jdbc:mysql://localhost/sampledb";
   static final String USER = "root";
   static final String PASS = "123456";
   static final String QUERY = "SELECT id, name, mobile FROM students";

   public static void main(String[] args) {
      // Open a connection
      try
	  {
		Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(QUERY);
		ResultSetMetaData rsmd=rs.getMetaData();
		System.out.println("Total columns: " + rsmd.getColumnCount());  
		System.out.println("Column Name of 1st column: " + rsmd.getColumnName(1));  
System.out.println("Column Type Name of 1st column: " + rsmd.getColumnTypeName(1)); 
      } 
	  catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

 

Output for the above program is given below:

Total columns: 3
Column Name of 1st column: id
Column Type Name of 1st column: INT

 

Note: The above compiles successfully only when we add the JAR file to the CLASSPATH environment variable as shown below:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


set CLASSPATH=.;C:\Soft\mysql-connector-j-9.1.0\mysql-connector-j-9.1.0.jar

 

For more information on JDBC, refer the following links:

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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