/******************************************************************** * 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 * This example is OBSOLETE and wrongly imports mysql classes * See InsertCoffees.java *******************************************************************/ import org.gjt.mm.mysql.*; // Warning: importing just mysql will not work ! import org.gjt.mm.mysql.Connection; import org.gjt.mm.mysql.Statement; import org.gjt.mm.mysql.Driver; import org.gjt.mm.mysql.ResultSet; import java.sql.*; public class InsertCoffees { public static void main(String args[]) { String username; String password; String url; String dropString; String createString; username = "nobody"; password = null; // 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 // 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 = (Connection) DriverManager.getConnection(url, username, password); System.out.println ("Ok, connection to the DB worked. Let's see if we can insert something:"); // Create a Statement Object stmt = (Statement) con.createStatement(); // Send the query and bind to the result set stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)"); stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Espresso', 150, 9.99, 0, 0)"); stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)"); stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)"); System.out.println ("4 Items have been inserted, you can now run the QueryCoffees program"); // 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(""); } } } }