import java.sql.*;

public class Create4JData
{

  static String[] SQLData =
  {
    "(1,  'John',   'Mon', 1, 'JustJoe')",
    "(2,  'JS',     'Mon', 1, 'Cappuccino')",
    "(3,  'Marie',  'Mon', 2, 'CaffeMocha')",
    "(4,  'Anne',   'Tue', 8, 'Cappuccino')",
    "(5,  'Holley', 'Tue', 2, 'MoJava')",
    "(6,  'jDuke',  'Tue', 3, 'Cappuccino')",
    "(7,  'Marie',  'Wed', 4, 'Espresso')",
    "(8,  'JS',     'Wed', 4, 'Latte')",
    "(9,  'Alex',   'Thu', 3, 'Cappuccino')",
    "(10, 'James',  'Thu', 1, 'Cappuccino')",
    "(11, 'jDuke',  'Thu', 4, 'JustJoe')",
    "(12, 'JS',     'Fri', 9, 'Espresso')",
    "(13, 'John',   'Fri', 3, 'Cappuccino')",
    "(14, 'Beth',   'Fri', 2, 'Cappuccino')",
    "(15, 'jDuke',  'Fri', 1, 'Latte')"
  };

  public static void main(String[] args)
  {

    Connection con = null;
    int iRowCount = 0;
    Statement stmt = null;

    // reference: the README file of MySQL Connector/J
    String sDriver =
      "com.mysql.jdbc.Driver";
    String sURL =
      "jdbc:mysql://DCCM831X/jguru";

    // remark: using MySQL control centre to create this account
    String sUsername = "blah";
    String sPassword = "blah";

    try   // Attempt to load the JDBC driver 
    {     // with newInstance
        Class.forName( sDriver ).newInstance();
    }
    catch( Exception e )  // error
    {
      System.err.println(
        "Failed to load current driver.");
      return;
    } // end catch

    try
    {

      con = DriverManager.getConnection (sURL+"?user="+sUsername+"&password="+
            sPassword);
      stmt = con.createStatement();
    }
    catch ( Exception e)
    {
      System.err.println( "problems connecting to ");
      System.err.println( e.getMessage() );

      if( con != null)
      {
        try { con.close(); }
        catch( Exception e2 ) {}
      }

      return;
    } // end catch

    // to allow the program to be run more than once,
    // attempt to remove the table from the database
    try
    {
      stmt.executeUpdate( "DROP TABLE JJJJData" );
      System.out.println(
        "Table JJJJData was removed.");
    }
    catch ( Exception e ) { /* don't care */ }


    // execute SQL commands 
    // to create table and insert data
    try
    {
      stmt.executeUpdate( "CREATE TABLE JJJJData (" +
             "Entry      INTEGER      NOT NULL, "   +
             "Customer   VARCHAR (20) NOT NULL, "   +
             "DOW        VARCHAR (3)  NOT NULL, "   +
             "Cups       INTEGER      NOT NULL, "   +
             "Type       VARCHAR (10) NOT NULL,"    +
             "PRIMARY KEY( Entry )"                 +
                                          ")" );

      System.out.println(
        "Table JJJJData was created.");

      for (int i = 0; i < SQLData.length; i++)
      {
        iRowCount += 
        stmt.executeUpdate( 
          "INSERT INTO JJJJData VALUES " + 
           SQLData[i] );
      }

      System.out.println( iRowCount + 
         " Rows inserted into JJJJData.");

    }
    catch ( Exception e )
    {
      System.err.println(
        "problem with SQL sent to " + sURL + ":" );
      System.err.println( e.getMessage() );
    }
    finally
    {
      try { stmt.close(); }
      catch( Exception e ) {}

      try { con.close(); }
      catch( Exception e ) {}
    } // end finally clause

  } // end main

} // end class Create4JData


