/* * Display data from any table. */ import java.sql.*; import java.io.*; class PrintTable { static void printColTypes(ResultSetMetaData rsmd) throws SQLException { int columns = rsmd.getColumnCount(); for (int i = 1; i <= columns; i++) { int jdbcType = rsmd.getColumnType(i); String name = rsmd.getColumnTypeName(i); System.out.print("Column " + i + " is JDBC type " + jdbcType); System.out.println(", which the DBMS calls " + name); } } public static void main(String args[]) throws SQLException, IOException { // Load the Oracle JDBC driver try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // Get UserName/Password BufferedReader inp = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter User Name:"); String user = inp.readLine(); System.out.print("Enter Password:"); String password = inp.readLine(); // Connect to the database. // "ora" is the machine with the oracle server. // SQL Listener is listening on port 1521. // The instance of the database is "csdb". Connection con = DriverManager.getConnection ("jdbc:oracle:thin:" + user.trim()+"/"+password.trim()+ "@ora.oswego.edu:1521:csdb"); Statement stmt; stmt = con.createStatement(); //get the table name System.out.print("Enter Table Name:"); String tname = inp.readLine(); String query = "select * from " + tname; ResultSet rs = stmt.executeQuery(query); ResultSetMetaData rsmd = rs.getMetaData(); printColTypes(rsmd); System.out.println(""); int numberOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numberOfColumns; i++) { if (i > 1) System.out.print(", "); String columnName = rsmd.getColumnName(i); System.out.print(columnName); } System.out.println(""); while (rs.next()) { for (int i = 1; i <= numberOfColumns; i++) { if (i > 1) System.out.print(", "); String columnValue = rs.getString(i); System.out.print(columnValue); } System.out.println(""); } stmt.close(); con.close(); } catch(SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } } }