smeacham

JDBC SELECT example

Sep 18th, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. Statement stmt = conn.createStatement();
  2. try {
  3.     ResultSet rs = stmt.executeQuery( "SELECT * FROM MyTable" );
  4.     try {
  5.         while ( rs.next() ) {
  6.             int numColumns = rs.getMetaData().getColumnCount();
  7.             for ( int i = 1 ; i <= numColumns ; i++ ) {
  8.                // Column numbers start at 1.
  9.                // Also there are many methods on the result set to return
  10.                //  the column as a particular type. Refer to the Sun documentation
  11.                //  for the list of valid conversions.
  12.                System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );
  13.             }
  14.         }
  15.     } finally {
  16.         try { rs.close(); } catch (Throwable ignore) { /* Propagate the original exception
  17. instead of this one that you may want just logged */ }
  18.     }
  19. } finally {
  20.     try { stmt.close(); } catch (Throwable ignore) { /* Propagate the original exception
  21. instead of this one that you may want just logged */ }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment