// Connect to the database connection = DriverManager.getConnection(URL, "scott", "tiger"); // Obtain a statement object statement = connection.createStatement();
// Execute the SQL String sql = "select * from JDBC_TEST"; ResultSet rs = statement.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1)); // 列的索引: 1-based } rs.close(); } // Don't try this at home, catch SQLException and all others catch( Exception e ) { e.printStackTrace(); } finally { // Time to close everthing up. if( statement != null ) { try { statement.close(); } catch( SQLException e ){ } // nothing we can do }
if( connection != null ) { try { connection.close(); } catch( SQLException e ){ } // nothing we can do } } } }