System.out.println( "Register driver: postgresql.Driver" );
try
/**/ { new postgresql.Driver();
}
catch( SQLException e )
{ System.out.println( "Exception in Driver Registration" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Get Connection to: jdbc:postgresql://ies11-49/ucjava" );
/**/ Connection con = null;
try
/**/ { con = DriverManager.getConnection( "jdbc:postgresql://ies11-49/ucjava", "ucjava", "" );
}
catch( SQLException e )
{ System.out.println( "Exception in Getting Connection" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Check for warnings from Connection" );
SQLWarning warning = new SQLWarning();
try
{ warning = con.getWarnings();
while( warning != null )
{ System.out.println( "Warning: " + warning );
warning = warning.getNextWarning();
}
}
catch( SQLException e )
{ System.out.println( "Exception in Getting Connection Warnings" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Create a statement" );
/**/ Statement stmt = null;
try
/**/ { stmt = con.createStatement();
}
catch( SQLException e )
{ System.out.println( "Exception in Creating a Statement" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Execute query: SELECT * FROM students;" );
/**/ ResultSet results = null;
try
/**/ { results = stmt.executeQuery( "SELECT * FROM students;" );
}
catch( SQLException e )
{ System.out.println( "Exception in Executing Query" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Process Results\n" );
/**/ displayResults( results );
System.out.println( "Execute update: INSERT INTO students;" );
try
/**/ { int count = stmt.executeUpdate( "INSERT INTO students VALUES( 'Paul', 'Thompson', 'iq1b3482' );" );
System.out.println( "" + count + " updates" );
}
catch( SQLException e )
{ System.out.println( "Exception in Executing Update" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
// Repeat query
System.out.println( "Execute query: SELECT * FROM students;" );
try
/**/ { results = stmt.executeQuery( "SELECT * FROM students;" );
}
catch( SQLException e )
{ System.out.println( "Exception in Executing Query" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Process Results\n" );
/**/ displayResults( results );
System.out.println( "Create a Prepared Statement: SELECT * FROM students WHERE firstname = ?;" );
/**/ PreparedStatement pStmt = null;
try
/**/ { pStmt = con.prepareStatement( "SELECT * FROM students WHERE firstname = ?;" );
}
catch( SQLException e )
{ System.out.println( "Exception in Creating a Prepared Statement" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Execute Prepared Statement with parameter: 'John'" );
try
/**/ { pStmt.setString( 1, "John" );
/**/ results = pStmt.executeQuery();
}
catch( SQLException e )
{ System.out.println( "Exception in Executing a Prepared Statement" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Process Results\n" );
/**/ displayResults( results );
System.out.println( "Execute Prepared Statement with parameter: 'Paul'" );
try
{ pStmt.setString( 1, "Paul" );
results = pStmt.executeQuery();
}
catch( SQLException e )
{ System.out.println( "Exception in Executing a Prepared Statement" );
while( e != null )
{ System.out.println( e );
e = e.getNextException();
}
System.exit(0);
}
System.out.println( "Process Results\n" );
/**/ displayResults( results );