Tuesday, 5 February 2013

RESULTSETMETADATA | Java J2EE Tutorial pdf

RESULTSETMETADATA

JDBC enables us to process a result set even if the database table columns are not specified in the SQL query. Imagine that we need to write a program that will accept any SQL select statement and display the retrieved data. The java.sql.ResultSetMetaData class can dynamically find out the structure of the underlying database table – how many columns it contains, and the types and names of the columns. Here’s an example:
import java.sql.*;
public class jdbc_type1_meta {
public static void main(String a[]) throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:TEST");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp");
ResultSetMetaData rsm=rs.getMetaData();
int count=rsm.getColumnCount();
System.out.println(count);
int i=1;
while(i<=count){
System.out.println(rsm.getColumnName(i)+" : "+
rsm.getColumnTypeName(i));
i++;
}
rs.close();
st.close();
con.close();
}
}
Output

No comments: