Advanced Java (2160707)

BE | Semester-4   Winter-2018 | 20-11-2018

Q3) (a)

Explain ResultSetMetaData with suitable program.

ResultsetMetaData Interface

  • Metadata means data about data.
  • If you have to get metadata of a table like
    1. total number of column
    2. column name
    3. column type etc.
  • ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object

Example: ResultSetMetaData

  1. import java.sql.*;
  2. public class MetadataDemo
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         try
  7.         {
  8.             Class.forName("com.mysql.jdbc.Driver");
  9.             Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/gtu", "root",“pwd");
  10.             Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
  11.             ResultSet rs = stmt.executeQuery("SELECT * from gtu");
  12.             ResultSetMetaData rsmd=rs.getMetaData();
  13.             System.out.println("Total columns:"+rsmd.getColumnCount());
  14.             System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));
  15.             System.out.println("Column Type Name of 1st column:“+rsmd.getColumnTypeName(1));
  16.             stmt.close();
  17.             conn.close();
  18.         }
  19.         catch(Exception e)
  20.         {
  21.             System.out.println(e.toString());
  22.         }
  23.     }//PSVM
  24. }//class

OUTPUT

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