import java.sql.*; public class InsertCoffees { public static void main(String args[]) { //This program assumes that the CafeJava database allready exists and containes //a table called "coffee". First, that table is filled with values, //then all values are read and printed to System.out. //This URL requires an ODBC link called CafeJava. String url = "jdbc:odbc:CafeJava"; Connection con; Statement stmt; //This SQL statement reads the contents of the "coffee" table. String query = "select COF_NAME, PRICE from COFFEES"; try { //Load the JDBC-ODBC bridge driver. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { //Open a connection to the database. con = DriverManager.getConnection(url, "sa", ""); //Create a statement. stmt = con.createStatement(); //Execute various SQL statements that inserts values //into the "coffees" table. stmt.executeUpdate("insert into COFFEES " + "values('Colombian', 00101, 7.99, 0, 0)"); stmt.executeUpdate("insert into COFFEES " + "values('French_Roast', 00049, 8.99, 0, 0)"); stmt.executeUpdate("insert into COFFEES " + "values('Espresso', 00150, 9.99, 0, 0)"); stmt.executeUpdate("insert into COFFEES " + "values('Colombian_Decaf', 00101, 8.99, 0, 0)"); stmt.executeUpdate("insert into COFFEES " + "values('French_Roast_Decaf', 00049, 9.99, 0, 0)"); //Read the contents of the "coffee" table. The String "query" //is defined above. ResultSet rs = stmt.executeQuery(query); System.out.println("Coffee Break Coffees and Prices:"); //Loop through the result set of the query and print all lines. while (rs.next()) { String s = rs.getString("COF_NAME"); float f = rs.getFloat("PRICE"); System.out.println(s + " " + f); } stmt.close(); con.close(); } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } } }