Write a java program which points the data of a table along with its column names?



import java.sql.*;
class Table {
 public static void main(String[] args) {
   try {
    DriverManager.registerDriver(new Sun.jdbc.odbc.JdbcOdbcDriver());
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
    System.out.println("CONNECTION ESTABLISHED...");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from dept");
    ResultSetMetaData rsmd = rs.getMetaData();
    System.out.println("===========================================");
    // PRINTING COLUMN NAME
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
     System.out.print(rsmd.getColumnName(i) + " ");
    }
    System.out.println("");
    System.out.println("==========================================");
    // PRINTING THE DATA OF THE TABLE
    while (rs.next()) {
     for (int j = 1; j <= rsmd.getColumnCount(); j++) {
      System.out.print(rs.getString(j) + " ");
     }
     System.out.println("");
    }
    con.close();
   } catch (SQLException sqle) {
    sqle.printStackTrace();
   }
  } // main
}; // Table


No comments:

Post a Comment