ResultSetMetaData in java
code:;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
class Test{
public static void main(String arg[]){
try(Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from stu");){
//create ResultSetMetaData obj
ResultSetMetaData rsmd = rs.getMetaData();
//get column count
int colCount = rsmd.getColumnCount();//3
//print column name
for(int i=1;i<=colCount;i++) {
System.out.print(rsmd.getColumnName(i)+" ");
}
System.out.println();
//for column type
for(int i=1;i<=colCount;i++) {
System.out.print(rsmd.getColumnTypeName(i)+" ");
}
System.out.println();
}catch(SQLException se) {
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment