
// SimpleJDBC.java
// Andrew Davison, October 22nd 2008, ad@fivedots.coe.psu.ac.th

// This program displays the firstnames and lastnames
// of the Authors table in the Books database.


import java.sql.*;


public class SimpleJDBC {

  public static void main(String[] args)
  {
    // The URL for the Books database.
    // It is 'protected' by a login and password.
    String url = "jdbc:odbc:Books";  
    String username = "anonymous";
    String password = "guest";

    try {
      // load the JDBC-ODBC Bridge driver
      Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

      // connect to the database using the DriverManager
      Connection conn = DriverManager.getConnection( url, username, password );

      // Create a statement set
      Statement statement = conn.createStatement();
       
      // Execute the SQL query
      ResultSet rs = statement.executeQuery("SELECT lastName, firstName FROM Authors" );

      // Print the result set
      while( rs.next() )
        System.out.println( rs.getString("lastName") + ", " +
                            rs.getString("firstName") );

      // Close down
      statement.close();
      conn.close();
    } 
    catch ( ClassNotFoundException cnfex ) {
      System.err.println("Failed to load JDBC/ODBC driver." );
      cnfex.printStackTrace();
      System.exit( 1 );  // terminate program
    }
    catch ( SQLException sqlex ) {
      System.err.println( sqlex );
      sqlex.printStackTrace();
    }
  } // end of main()

} // end of SimpleJDBC class


