JDBC ResultSetMetaData example

The simple meaning of metadata is data about data. There are two metadata available in the JDBC API - ResultSetMetaData and DatabaseMetaData.  

ResultSetMetaData

- I am describing the ResultSetMetaData. It is used to make descriptive information about ResultSet object, like; number of columns, name of columns and dataype of columns.

It does not provide any information regarding database and how many rows are available in the ResultSet object. Whether ResultSet is read only, updatable or scrollable.

First we have to create object of ResultSetMetadata by calling getMetaData() method from ResultSet object.

Syntax : ResultSetMetaData rsmd=res.getMeataData(); // res is a valid object of ResultSet object

The following are common methods in ResultSetMetadata interface -

  • int getColumnCount()
  • String getColumnName()
  • int getColumnType()
  • String getTableName()

The following is the source code to demostrates ResultSetMetaData interface. Here user will input table name and display the result with column heading :  

import java.sql.*;
import java.io.*;
public class ResultSetMetaDataDemo1 
{
Connection con;
public ResultSetMetaDataDemo1() 
{
    try 
    {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/
    test?user=root&password=root");
  
  catch (Exception e
  {
    System.out.println("Error in connection" + e);
  }
}
public void displyRecords(String tableName)
{
  String columnHeading="";
  try 
  {
  Statement stmt=con.createStatement();
  ResultSet res=stmt.executeQuery("select * from "+tableName.trim());
  if(res.next())
  {
  ResultSetMetaData rsmd=res.getMetaData();
  int columnCount=rsmd.getColumnCount();
  for (int i = 1; i<= columnCount; i++) {
  columnHeading=columnHeading+"\t"+rsmd.getColumnName(i);
  }
  System.out.println(columnHeading);
  while(res.next())
  {
  for (int i = 1; i<= columnCount; i++
  {
    System.out.print("\t"+res.getString(i));
  
  }
  System.out.println("\n");
  }
  }
  else
  System.out.println("There is no records in table");
  
  catch (Exception e
  {
  e.printStackTrace();
  }
}
public static void main(String[] args
{
ResultSetMetaDataDemo1 obj=new ResultSetMetaDataDemo1();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String choice="";
try
{
  do{
  System.out.println("Enter table name to display records");
  String name=br.readLine();
  obj.displyRecords(name);
  System.out.println("Do you wanto to continue(yes)");
  choice=br.readLine();
  }while(choice.trim().equals("yes"));
  
}
catch(Exception e)
{
  e.printStackTrace();
}
}
}
Download

Previous Next

Comment by : Fullington October 3rd, 2015
Comment : Arcitles like this just make me want to visit your website even more.

Comment by : Dhruti September 15th, 2014
Comment : plesae tell me resultset and resultsetmetadat are smae.

Comment by : prakash February 10th, 2013
Comment : will u please clarify more


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code