
// BuildTables.java
// Andrew Davison, October 22nd 2008, ad@fivedots.coe.psu.ac.th

// Create a new table, urlInfo, inside the Books.mdb database
// The table has three columns: id, name, url

import java.sql.*;


public class BuildTables {

  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";

    // SQL table creation and insert statements
    String[] SQLStats = {
      "create table urlInfo (id int, name char(48), url char(80))",
      "insert into urlInfo values(1, 'Andrew D', 'http://fivedots.coe.psu.ac.th/~ad')",
      "insert into urlInfo values(2, 'JavaSoft Home Page', 'http://www.javasoft.com')",
      "insert into urlInfo values(3, 'PSU', 'http://www.psu.ac.th')"
						};

    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();
       
      // Create urlInfo table
      for (int i = 0; i < SQLStats.length; i++) {
	      statement.executeUpdate(SQLStats[i]);
        System.out.println("Processed: " + SQLStats[i]);
      }
        
      // 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 BuildTables class

