Explain ResultSetMetaData with suitable program.
ResultsetMetaData Interface
-
Metadata means data about data.
-
If you have to get metadata of a table like
-
total number of column
-
column name
-
column type etc.
-
ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object
Example: ResultSetMetaData
- import java.sql.*;
- public class MetadataDemo
- {
- public static void main(String[] args)
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/gtu", "root",“pwd");
- Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
- ResultSet rs = stmt.executeQuery("SELECT * from gtu");
- 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));
- stmt.close();
- conn.close();
- }
- catch(Exception e)
- {
- System.out.println(e.toString());
- }
- }//PSVM
- }//class
OUTPUT
Total columns: 3
Column Name of 1st column:Enr_no
Column Type Name of 1st column:INT