// Robert Irwin
// mcaiyri3
// Main.java Exercise 4.1

import java.sql.*;

public class Main
{
  // The data to be entered
  static String[] SQLData =
  { "(1,  'John', 'Mon',  1,  'ArabianMocha')",
    "(2,  'Jim',  'Mon',  1,  'Cappuccino')",
    "(3,  'Marie',  'Mon',  2,  'ArabianMocha')",
    "(4,  'Anne', 'Tue',  8,  'Cappuccino')",
    "(5,  'Holly',  'Tue',  2,  'ItalianRoast')",
    "(6,  'Paul', 'Tue',  3,  'Cappuccino')",
    "(7,  'Marie',  'Wed',  4,  'Espresso')",
    "(8,  'JS', 'Wed',  4,  'ItalianRoast')",
    "(9,  'Alex', 'Thu',  3,  'Cappuccino')",
    "(10, 'James',  'Thu',  1,  'Cappuccino')",
    "(11, 'Paul', 'Thu',  4,  'Espresso')",
    "(12, 'Jim',  'Fri',  9,  'Espresso')",
    "(13, 'John', 'Fri',  3,  'Cappuccino')",
    "(14, 'Beth', 'Fri',  2,  'Cappuccino')",
    "(15, 'Paul', 'Fri',  1,  'ItalianRoast')",};

  public static void main(String[] args)
  {
    Connection con = null;
    int iRowCt = 0;
    Statement state = null;

    String sURL = "jdbc:odbc:repository";

    try // Try to load the JDBC sun driver
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
      catch( Exception e )  // Catches error
      {
      System.out.println("Failed to load current driver.");
      return;
      } // End catch

    try
    {
    con = DriverManager.getConnection (sURL);
    state = con.createStatement();
    }
      catch ( Exception e)
      {
      System.out.println( "problems connecting to " + sURL + ":" );
      System.out.println( e.getMessage() );

      if( con != null)
        {
        try { con.close(); }
        catch( Exception e2 ) {}
        }
      return;
      } // End catch

    // Allows the program to run more than once so table is removed
    try
    {
    state.executeUpdate( "DROP TABLE JJJJData" );
    System.out.println("Table JJJJData was removed.");
    }
      catch ( Exception e ) {}

    // SQL commands run to make the table
    try
    {
    state.executeUpdate( "CREATE TABLE JJJJData (" +
    "Entry      INTEGER      NOT NULL, "   +
    "Customer   VARCHAR (7) NOT NULL, "   +
    "DOW        VARCHAR (3)  NOT NULL, "   +
    "Cups       INTEGER      NOT NULL, "   +
    "Type       VARCHAR (13) NOT NULL,"    +
    "PRIMARY KEY( Entry )"                 +
    ")" );
    // Message for successful creation
    System.out.println("Table JJJJData was created.");

      // For loop
      for (int j = 0; j < SQLData.length; j++)
      { iRowCt +=
        state.executeUpdate(
        "INSERT INTO JJJJData VALUES " +
        SQLData[j] );
      } // End for loop

      System.out.println( iRowCt + " Rows inserted into JJJJData.");

    }
      catch ( Exception e )
      {
      System.out.println("problem with SQL sent to " + sURL + ":" );
      System.out.println( e.getMessage() );
      }  // End catch

    // The finally clause
    finally {
      try { state.close(); }
      catch( Exception e ) {}
      try { con.close(); }
      catch( Exception e ) {} } // end finally clause

  } // End main
} // End class

