JDBC Resultsetmetadata Interface
Advertisements
Resultsetmetadata Interface in JDBC
The metadata means data about data, in case of database we get metadata of a table like total number of column, column name, column type etc.
While executing a select operation on a database if the table structure is already known for the programmer, then a programmer of JDBC can read the data from ResutlSet object directly. If the table structure is unknown then a JDBC programmer has to take the help of ResultSetMetadata.
A ResultSetMetaData reference stores the metadata of the data selected into a ResutlSet object.
How to get the object of ResultSetMetaData ?
To obtain a object of ResultSetMetaData, we need to call getMetaData() method of ResutlSet object..
Syntax
ResultSetMetaData rsmd=rs.getMetaData();
Methods of ResultSetMetaData
The following are the methods called on ResultSetMetaData reference.
method | Discription | |
---|---|---|
1 | getColumnCount() | To find the number of columns in a ResultSet |
2 | getColumnName() | To find the column name of a column index. |
3 | getColumnTypeName() | To find data type of a column. |
4 | getColumnDisplaySize() | To find size of a column. |
Example of ResultSetMetaData
import java.sql.*; class ResmataDataTest { public static void main(String[] args)throws Exception { Class.forName("oracle.jdbc.OracleDriver"); Connection con =DriverManager.getConnection("jdbc:oracle:thin:@John-pc:1521:xe","system","system"); System.out.println("driver is loaded"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from student"); ResultSetMetaData rsmd=rs.getMetaData(); //find the no of columns int count=rsmd.getColumnCount(); for(int i=1;i <=count;i++) { System.out.println("column no :"+i); System.out.println("column name :"+rsmd.getColumnName(i)); System.out.println("column type :"+rsmd.getColumnTypeName(i)); System.out.println("column size :"+rsmd.getColumnDisplaySize(i)); System.out.println("-----------"); } rs.close(); stmt.close(); con.close(); } }
Google Advertisment