/******************************************************************** * File: http://tecfa.unige.ch/guides/java/staf2x/ex/jdbc/coffee-break/CreateCoffeesTwz1.java * Made by Daniel.Schneider@tecfa.unige.ch 1/1999 TECFA. * This is Freeware * * SUN Java Tutorial JDBC Example adapted to MySQL * Twz1 MySQL Driver Version -> http://www.voicenet.com/~zellert/tjFM/ * At TECFA (UNIX) : source /local/env/java117.csh * setenv CLASSPATH /local/java/classes/:. * Other Sites: install the Driver * * WARNING: This example will NOT work as is. You need a username and password * *******************************************************************/ import twz1.jdbc.mysql.*; import java.sql.*; public class CreateCoffeesTwz1 { public static void main(String args[]) { String username; String password; String url; String dropString; String createString; // ---- configure START username = "YOUR_NAME"; password = "YOUR_PASSWORD"; // The URL that will connect to TECFA's MySQL server // Syntax: jdbc:TYPE:machine:port/DB_NAME url = "jdbc:z1MySQL://tecfa.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("twz1.jdbc.mysql.jdbcMysqlDriver"); } 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 = (Connection) 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 = (Statement) 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(""); } } } }