What is ResultSetMetaData?

It is used to get the information about the ResultSet();
getColumnCount()- To know the no. of columns
getColumnSize()
getColumnType()
getColumnDisplaySize()
getColumnName()
Example:To get all the values when number of columns is unknown
import java.sql.*;
class demo
{
public static void main(String[] args){
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","oracle");
stmt=con.createStatement();
rs=stmt.executeQuery("select *from emp");
ResultSetMetaData rmd=rs.getMetaData();
String name=rmd.getTableName(1);
System.out.println(name+" table values are:");
while(rs.next()){

for(int i=1;i<=rmd.getColumnCount();i++){
System.out.print(rs.getString(i)+"\t");
}
System.out.println();
}}catch(Exception e){
System.out.println("the exception is::::->"+e);
}
finally{
try{
rs.close();
stmt.close();
con.close();}
catch(Exception e){
System.out.println("2nd try block exception:::->"+e);
}}}};

No comments:

Post a Comment