The Java Program: Create.java

  1 import java.sql.Driver;
  2 import java.sql.DriverManager;
  3 import java.sql.DriverPropertyInfo;
  4 import java.sql.Statement;
  5 import java.sql.Connection;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.util.*;
  9 
 10 /*
 11   Make sure drivers are on classpath "-classpath path1:path2
 12   -Djdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver:org.gjt.mm.mysql.Driver
 13 */
 14 public class Create {
 15    final static String mm_driver ="org.gjt.mm.mysql.Driver";
 16    final static String url = "jdbc:mysql://localhost/test?user="+System.getProperty("user.name");
 17    final static String del = "DROP TABLE COFFEES";
 18    final static String create =
 19       "CREATE TABLE COFFEES (COF_NAME VARCHAR(32), SUB_ID INTEGER, PRICE FLOAT, SALES INTEGER, TOTAL INTEGER)";
 20    final static String add_co = "INSERT INTO COFFEES VALUES ('Colombian', 101, 7.99, 0, 0)";
 21    final static String add_fr = "INSERT INTO COFFEES VALUES ('French_Roast', 49, 8.99, 0, 0)";
 22    final static String add_tu = "INSERT INTO COFFEES VALUES ('Expresso', 150, 9.99, 0, 0)";
 23 
 24    public static void main (String [] args) {
 25       try {
 26          // Some JVMs get confused, so use "newInstance()"
 27          Class.forName(mm_driver).newInstance();
 28          final Connection c = DriverManager.getConnection(url);
 29          final Statement stmt = c.createStatement ();
 30          int rows = stmt.executeUpdate (create);
 31          rows = stmt.executeUpdate (add_co);
 32          rows = stmt.executeUpdate (add_fr);
 33          rows = stmt.executeUpdate (add_tu);
 34          rows = stmt.executeUpdate (del);
 35          c.close();
 36             
 37       } catch (SQLException e) {
 38          System.out.println("SQLException: " + e.getMessage());
 39          System.out.println("SQLState:     " + e.getSQLState());
 40          System.out.println("VendorError:  " + e.getErrorCode());
 41       } catch (ClassNotFoundException e) {
 42          e.printStackTrace();
 43       } catch (InstantiationException e) {
 44          e.printStackTrace();
 45       } catch (IllegalAccessException e) {
 46          e.printStackTrace();
 47       }
 48    }
 49 
 50 }