A.4 Populate the MySQL Database Used in Core JSTL Examples

   

A.4 Populate the MySQL Database Used in Core JSTL Examples

After you have downloaded and installed the MySQL DBMS and the associated JDBC driver and have created the database used in this book, as discussed in the preceding sections, you need to populate that database. If you download this book's source code, you will find instructions on how to populate that database in the README file in the top-level directory that's created when you unjar the JAR file containing the book's source code. Those instructions tell you to run a Java program that populates the core-jstl database. Once you have populated the database, you are ready to run all of the examples in the book that use that database.

Although it's not necessary to understand the Java program that creates the Core JSTL database, that program is listed in Listing 1 for completeness.

Listing A.1 Populating the core-jstl Database
 import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.SQLException; public class CreateDB {    private Connection conn;    private Statement stmt;    public static void main(String args[]) {       new CreateDB();    }    public CreateDB() {       try {          loadJDBCDriver();          conn = getConnection("core-jstl");          stmt = conn.createStatement();          createTables(stmt);          populateTables(stmt);          stmt.close();          conn.close();       }       catch(SQLException ex) {          ex.printStackTrace();       }    }    private void createTables(Statement stmt) {       System.out.println("Creating Tables");       try {          stmt.execute("CREATE TABLE Customers (" +                       "Cust_ID         INTEGER, " +                       "Name            VARCHAR(25), " +                       "Phone_Number    VARCHAR(25), " +                       "Street_Address  VARCHAR(50), " +                       "City            VARCHAR(50), " +                       "State           VARCHAR(30))");          stmt.execute("CREATE TABLE Orders (" +                       "Order_Number    INTEGER, " +                       "Order_Date      DATE, "    +                       "Cust_ID         INTEGER, " +                       "Amount          FLOAT, "   +                       "Description     VARCHAR(30))");          stmt.execute("CREATE TABLE Accounts (" +                       "Cust_ID         INTEGER, " +                       "Balance         FLOAT)");       }       catch(SQLException ex) {          ex.printStackTrace();       }    }    private void populateTables(Statement stmt) {       System.out.println("Populating Tables");       try {          stmt.execute("INSERT INTO Customers VALUES " +             "(1,  'William Dupont', '(652)488-9931', " +             "'801 Oak Street', 'Eugene', 'Nebraska')," +             "(2,  'Anna Keeney', '(716)834-8772', " +             "'86 East Amherst Street', 'Buffalo', 'New York')," +             "(3,  'Mariko Randor', '(451)842-8933', " +             "'923 Maple Street', 'Springfield', 'Tennessee')," +             "(4,  'John Wilson', '(758)955-5934', " +             "'8122 Genessee Street', 'El Reno', 'Oklahoma')," +             "(5,  'Lynn Seckinger', '(552)767-1935', " +             "'712 Kehr Street', 'Kent', 'Washington')," +             "(6,  'Richard Tattersall', '(455)282-2936', " +             "'21 South Park Drive', 'Dallas', 'Texas')," +             "(7,  'Gabriella Sarintia', '(819)152-8937', " +             "'81123 West Seneca Street', 'Denver', 'Colorado')," +             "(8,  'Lisa Hartwig', '(818)852-1937', " +             "'6652 Sheridan Drive', 'Sheridan', 'Wyoming')," +             "(9,  'Shirley Jones', '(992)488-3931', " +             "'2831 Main Street', 'Butte', 'Montana')," +             "(10, 'Bill Sprague', '(316)962-0632', " +             "'1043 Cherry Street', 'Cheektowaga', " +             "'New York')," +             "(11, 'Greg Doench', '(136)692-6023', " +             "'99 Oak Street', 'Upper Saddle River', " +             "'New Jersey')," +             "(12, 'Solange Nadeau', '(255)767-0935', " +             "'177 Rue St. Catherine', 'Montreal', 'Quebec')," +             "(13, 'Heather McGann', '(554)282-0936', " +             "'7192 913 West Park', 'Buloxie', 'Mississippi')," +             "(14, 'Roy Martin', '(918)888-0937', " +             "'5571 North Olean Avenue', 'White River', " +             "'Arkansas')," +             "(15, 'Claude Loubier', '(857)955-0934', " +             "'1003 Rue de la Montagne', " +             "'St. Marguerite de Lingwick', 'Quebec')," +             "(16, 'Dan Woodard', '(703)555-1212', " +             "'2993 Tonawonda Street', 'Springfield', " +             "'Missouri')," +             "(17, 'Ron Dunlap', '(761)678-4251', " +             "'5579 East Seneca Street', 'Kansas City', " +             "'Kansas')," +             "(18, 'Keith Frankart', '(602)152-6723', " +             "'88124 Milpidas Lane', 'Springfield', 'Maryland')," +             "(19, 'Andre Nadeau', '(541)842-0933', " +             "'94219 Rue Florence', " +             "'St. Marguerite de Lingwick', 'Quebec')," +             "(20, 'Horace Celestin', '(914)843-6553', " +             "'99423 Spruce Street', 'Ann Arbor', 'Michigan')");          stmt.execute("INSERT INTO Orders VALUES " +  "(1,  '2002-05-20', '1', '129.99', 'Wristwatch'), " +  "(2,  '2002-05-21', '1', '19.95',  'Coffee grinder'), " +  "(3,  '2002-05-24', '1', '29.76',  'Bath towel'), " +  "(4,  '2002-05-23', '1', '39.34',  'Deluxe cheese grater'), " +  "(5,  '2002-05-22', '2', '56.75',  'Champagne glass set'), " +  "(6,  '2002-05-20', '2', '28.11',  'Instamatic camera'), " +  "(7,  '2002-05-22', '2', '38.77',  'Walkman'), " +  "(8,  '2002-05-21', '2', '56.76',  'Coffee maker'), " +  "(9,  '2002-05-23', '2', '21.47',  'Car wax'), " +  "(10, '2002-05-21', '2', '16.80',  'Tape recorder'), " +  "(11, '2002-05-24', '2', '25.44',  'Art brush set'), " +  "(12, '2002-05-22', '3', '47.63',  'Game software'), " +  "(13, '2002-05-23', '3', '93.96',  'Furby collection'), " +  "(14, '2002-05-20', '4', '81.27',  'CD Player'), " +  "(15, '2002-05-21', '4', '66.83',  'Microphone'), " +  "(16, '2002-05-23', '4', '75.91',  'Dish set'), " +  "(17, '2002-05-22', '4', '32.67',  'Electric toothbrush'), " +  "(18, '2002-05-24', '5', '17.45',  'Case of mouthwash'), " +  "(19, '2002-05-20', '5', '88.81',  'Vacuum cleaner'), " +  "(20, '2002-05-21', '5', '29.13',  'Fine chocolates'), " +  "(21, '2002-05-21', '5', '77.02',  'Computer monitor arm'), " +  "(22, '2002-05-22', '6', '119.11', 'Tennis shoes'), " +  "(23, '2002-05-23', '7', '107.96', 'Beanie Baby collection'), " +  "(24, '2002-05-22', '7', '101.97', 'Humidifier'), " +  "(25, '2002-05-22', '7', '223.55', 'Exercise bike'), " +  "(27, '2002-05-24', '7', '49.42',  'Coffee maker')");          stmt.execute("INSERT INTO Accounts VALUES " +             "(1,  '1245.97'), "   + "(2,  '130400.00'), " +             "(3,  '28745.88'), "  + "(4,  '125863.32'), " +             "(5,  '25.99'), "     + "(6,  '891.34'), "    +             "(7,  '924.76'), "    + "(8,  '1578.22'), "   +             "(9,  '1258.77'), "   + "(10, '259876.33'), " +             "(11, '125866.09'), " + "(12, '159.88'), "    +             "(13, '590886.22'), " + "(14, '678231.44'), " +             "(15, '245.33'), "    + "(16, '8999.06'), "   +             "(17, '1342.82'), "   + "(18, '197312.33'), " +             "(19, '907310.21'), " + "(20, '921345.33')");       }       catch(SQLException ex) {          ex.printStackTrace();       }    }    private void loadJDBCDriver() {       try {          Class.forName("org.gjt.mm.mysql.Driver").newInstance();       }       catch(Exception e) {          e.printStackTrace();       }    }    private Connection getConnection(String dbName) {       Connection con = null;       try {          con = DriverManager.getConnection(                "jdbc:mysql://localhost/" + dbName);       }       catch(SQLException sqe) {          System.out.println("Couldn't access database " + dbName);       }       return con;    } } 

The preceding Java program loads the MySQL JDBC driver and creates a connection to the core-jstl database. Subsequently, the program creates and populates the tables for that database.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net