import java.sql.* ; class JDBC_sample { public static void main( String args[] ) { try { // Load the database driver Class.forName( "oracle.jdbc.driver.OracleDriver" ) ; // Get a connection to the database Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@apollo.vse.gmu.edu:1521:ite10g", "XXX", "YYY" ) ; // Print all warnings for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() ) { System.out.println( "SQL Warning:" ) ; System.out.println( "State : " + warn.getSQLState() ) ; System.out.println( "Message: " + warn.getMessage() ) ; System.out.println( "Error : " + warn.getErrorCode() ) ; } // Get a statement from the connection Statement stmt = conn.createStatement() ; // Execute the query ResultSet rs = stmt.executeQuery( "SELECT sid, sname, rating FROM Sailors" ) ; System.out.println("All sailors (using Statement object):"); // Loop through the result set while( rs.next() ) System.out.println( rs.getString(1) + " " + rs.getString(2) + " " + rs.getInt(3)) ; // Demonstrate PreparedStatement String sql = "SELECT sid, sname, rating FROM Sailors WHERE rating > ?"; PreparedStatement pStmt = conn.prepareStatement(sql); pStmt.setInt(1, 5); ResultSet rs2 = pStmt.executeQuery(); System.out.println("Sailors with ratings > 5 (using PreparedStatement)"); // Loop through the result set while( rs2.next() ) System.out.println( rs2.getString(1) + " " + rs2.getString(2) + " " + rs2.getInt(3)) ; // Close the result set, statement and the connection rs.close() ; rs2.close(); stmt.close() ; pStmt.close(); conn.close() ; } catch( SQLException se ) { System.out.println( "SQL Exception:" ) ; // Loop through the SQL Exceptions while( se != null ) { System.out.println( "State : " + se.getSQLState() ) ; System.out.println( "Message: " + se.getMessage() ) ; System.out.println( "Error : " + se.getErrorCode() ) ; se = se.getNextException() ; } } catch( Exception e ) { System.out.println( e ) ; } } }