ResultSetMetaData:
The metadata means data about data i.e. we can get further information from the data. If you have to get information of a table like total number of column, column name, column type etc. ResultSetMetaData interface is useful because it provides methods to get these information from the ResultSet object.
Methods of ResultSetMetaData interface
public int getColumnCount()throws SQLException: it returns the total number of columns in the ResultSet object.
public String getColumnName(int index)throws SQLException: it returns the column name of the specified column index.
public String getColumnTypeName(int index)throws SQLException: it returns the column type name for the specified index.
public String getTableName(int index)throws SQLException: it returns the table name for the specified column index.
How to get the object of ResultSetMetaData:
The getMetaData() method of ResultSet interface returns the object of ResultSetMetaData.
Syntax
public ResultSetMetaData getMetaData()throws SQLException
Simple Example of ResultSetMetaData interface :
import java.sql.*;
class ResultSetMd {
public static void main(String []args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement("select * from employee");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Total Number of 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));
con.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
Output: Total Number of columns: 3
Column Name of 1st column: ID
Column Type Name of 1st column: NUMBER
To see DatabaseMetaData Interface click on link
The metadata means data about data i.e. we can get further information from the data. If you have to get information of a table like total number of column, column name, column type etc. ResultSetMetaData interface is useful because it provides methods to get these information from the ResultSet object.
Methods of ResultSetMetaData interface
public int getColumnCount()throws SQLException: it returns the total number of columns in the ResultSet object.
public String getColumnName(int index)throws SQLException: it returns the column name of the specified column index.
public String getColumnTypeName(int index)throws SQLException: it returns the column type name for the specified index.
public String getTableName(int index)throws SQLException: it returns the table name for the specified column index.
How to get the object of ResultSetMetaData:
The getMetaData() method of ResultSet interface returns the object of ResultSetMetaData.
Syntax
public ResultSetMetaData getMetaData()throws SQLException
Simple Example of ResultSetMetaData interface :
import java.sql.*;
class ResultSetMd {
public static void main(String []args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement("select * from employee");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Total Number of 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));
con.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
Output: Total Number of columns: 3
Column Name of 1st column: ID
Column Type Name of 1st column: NUMBER
To see DatabaseMetaData Interface click on link
No comments:
Post a Comment