/******************************************************************** * Made by Daniel.Schneider@tecfa.unige.ch 1999 TECFA. * This is Freeware * * Java Tutorial JDBC Example adapted to MySQL * MM MySQL Driver Version -> http://www.worldserver.com/mm.mysql/ * At TECFA (UNIX) : source /local/env/java117.csh * setenv CLASSPATH /local/java/classes/mm-jdbc/mysql.jar:. * Other Sites: install the Driver * * WARNING: This example will NOT work as is. You need a username and password * * Updated Feb 2000 (fixed imports and killed type casts) *******************************************************************/ // Warning: Do NOT import MySQL driver classes // If you do, you have to use type casts all over the place import java.sql.*; public class CreateCoffeesMM { public static void main(String args[]) { String username; String password; String url; String dropString; String createString; // ---- configure START username = "YOUR_USERNAME_HERE"; password = "YOUR_PASSWORD_HERE"; // The URL that will connect to TECFA's MySQL server // Syntax: jdbc:TYPE:machine:port/DB_NAME url = "jdbc:mysql://tecfa2.unige.ch:3306/COFFEEBREAK"; // ---- configure END // 2 SQL Statements we will send to the COFFEEBREAK db dropString = "drop table COFFEES "; createString = "create table COFFEES " + "(COF_NAME VARCHAR(32), " + "SUP_ID INTEGER, " + "PRICE FLOAT, " + "SALES INTEGER, " + "TOTAL INTEGER)"; // INSTALL/load the Driver (Vendor specific Code) try { Class.forName("org.gjt.mm.mysql.Driver"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { Connection con; Statement stmt; // Establish Connection to the database at URL with usename and password con = DriverManager.getConnection(url, username, password); System.out.println ("Let's see if can drop and recreate the COFFEE table in the COFFEEBREAK db:"); // Create a Statement Object (used to execute simple SQL statements) stmt = con.createStatement(); // Send 2 statements (works for INSERT, UPDATE, DELETE, DROP) stmt.executeUpdate(dropString); System.out.println ("COFFE table dropped (hopefully)"); stmt.executeUpdate(createString); System.out.println ("COFFE table created again (hopefully)"); // Close resources stmt.close(); con.close(); } // print out decent erreur messages catch(SQLException ex) { System.err.println("==> SQLException: "); while (ex != null) { System.out.println("Message: " + ex.getMessage ()); System.out.println("SQLState: " + ex.getSQLState ()); System.out.println("ErrorCode: " + ex.getErrorCode ()); ex = ex.getNextException(); System.out.println(""); } } } }