import java.sql.*; // Notice, do not import com.mysql.jdbc.* // or you will have problems! public class Test4 { Connection conn; /* Print stdout some informations about the database connection */ private void printInfo(Connection c) throws Exception { // Get meta-data about the database DatabaseMetaData info = c.getMetaData(); System.out.println("\nConnected to :\t" + info.getURL()); System.out.println("Driver :\t" + info.getDriverName()); System.out.println("Version :\t" + info.getDriverVersion()); } /* Print stdout all pending SQLWarning warnings */ private boolean checkForSQLWarnings(SQLWarning w) throws SQLException { boolean warning = false; if(w != null) { warning = true; System.out.println("\n**** Warning ****\n"); while(w != null) { System.out.println("SQLState: " + w.getSQLState()); System.out.println("Message: " + w.getMessage()); System.out.println("Vendor: " + w.getErrorCode()); System.out.println(""); w = w.getNextWarning(); } } return warning; } /* Print stderr all pending SQLException exceptions */ private void printSQLErrors(SQLException e) { while(e != null) { System.err.println("SQLState: " + e.getSQLState()); System.err.println("Message: " + e.getMessage()); System.err.println("Vendor: " + e.getErrorCode()); System.err.println(""); e = e.getNextException(); } } public void loadDriverAndConnect() throws Exception { try { // Load the jdbc driver for MySQL Class.forName("com.mysql.jdbc.Driver"); // Get a connection to the database source using Thin JDBC driver String url = "jdbc:mysql://localhost:3306/test"; conn = DriverManager.getConnection(url, "root", ""); // Print stdout warning messages if necessary checkForSQLWarnings(conn.getWarnings()); // Print stdout info messages printInfo(conn); } catch(SQLException e) { System.err.println("\n*** SQLException caught in LoadDriver()"); printSQLErrors(e); throw e; } catch(Exception e) { // This exception is caught if JDBC driver used cannot be loaded System.err.println("\n*** Exception caught in LoadDriver()"); throw e; } } /** * Close the connection to the data base source */ public void close() throws Exception { try { conn.close(); System.out.println("Test : Disconnecting ..."); } catch(Exception e) { System.err.println("\n*** Exception caught in close()"); throw e; } } public void createTablesAndInit() throws Exception { Statement stmt = conn.createStatement(); String creerTableCafe = "CREATE TABLE CAFE" + "(NOM_CAFE VARCHAR(32), FO_ID INTEGER, PRIX FLOAT, " + "VENTES INTEGER, TOTAL INTEGER)"; stmt.executeUpdate(creerTableCafe); stmt.executeUpdate("INSERT INTO CAFE VALUES ('Colombian', 101, 7.99, 0, 0)"); stmt.executeUpdate("INSERT INTO CAFE VALUES ('Espresso', 150, 9.99, 0, 0)"); stmt.executeUpdate("INSERT INTO CAFE VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)"); stmt.executeUpdate("INSERT INTO CAFE VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)"); } } class TestMain { public static void main(String[] args) { Test4 t = new Test4(); try{ t.loadDriverAndConnect(); t.createTablesAndInit(); t.close(); } catch(Exception e) { System.err.println("\n*** SQLException caught in main()"); } } }