Section 16.7. WEB-INFclassescomcybertrailsstore

   

16.7 WEB-INF/classes/com/cybertrails/store

Because we are keeping our tags in packages, we need to have the Item.java and Order.java listings in this folder. These two classes are not beans or tags. They are classes that stand in for the objects (items and orders) that appear in the real world. As we talked about in Chapter 5, it is generally good design practice to have classes representing the objects in the real world and then define methods for what can be done to and with those things.

Java on the Web still maintains the same principle, of course, but with the restrictions of HTML, such as with forms, things work a little differently. It's not really necessary, for instance, to create a "Customer" class. While a customer is an important (actually, necessary) part of the application, the Web allows us to just hit the database from the session, which is populated from the forms, and this would perhaps be more trouble than it is worth. We could have, I suppose, stored the billing information in an object instead of the session. But the shipping information is not necessarily part of the customer, and so we'd have to figure out something new to do at that stage. There's no point for this application to create an entire object to store customer information that will never be referenced by the application again (that's assuming that the back end order retrieval happens as part of another application).

16.7.1 Item.java

[View full width]
 
[View full width]
/* * Item.java */ package com.cybertrails.store; import java.util.*; import java.sql.*; public class Item { private String productID; private String productName; private String qty; private String price; private ArrayList options; private ArrayList priceAdds; private ArrayList optionIDs; private int optionid; private String error; private Connection con; /** CONSTRUCTOR */ public Item(String productID, String productName, ArrayList options, ArrayList priceAdds, ArrayList optionIDs, String q, String p) { this.productID = productID; this.productName = productName; this.options = options; this.priceAdds = priceAdds; this.optionIDs = optionIDs; this.qty = q; this.price = setPrice(); } public void setProductID(String p) { this.productID = p; } public void setProductName(String n) { this.productName = n; } public void setQty(String q) { this.qty = q; } public void setOptions(ArrayList o) { this.options = o; } public void setPriceAdds(ArrayList p) { this.priceAdds = p; } public String getProductID() { return productID; } public String getProductName() { return productName; } public String getQuantity() { return qty; } public ArrayList getOptions() { return options; } public ArrayList getOptionIDs() { return optionIDs; } public double getAdjustedPrice(){ double adjustedPrice = 0.0; for(int x = 0; x < priceAdds.size(); x++) { if (priceAdds.get(x) != null) { adjustedPrice += Double.parseDouble(priceAdds.get(x).toString()); } } return (adjustedPrice + Double.parseDouble(price)); } // get product total price // by multiplying quantity times adjusted price public double getFinalProductPrice(double x) { double p = x * Integer.parseInt(getQuantity()); return p; } // ******** setPrice() public String setPrice() { try { if (con == null) { connect(); } ResultSet rs; PreparedStatement getPrice; String s = new String("SELECT price FROM products WHERE productID = ?;"); getPrice = con.prepareStatement(s); // in JDBC, the first ? is index 1 (not 0) getPrice.setInt(1, Integer.parseInt(getProductID())); rs = getPrice.executeQuery(); while (rs.next()) { price = rs.getString("price"); } } catch (SQLException sqle) { sqle.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return price; } // end setPrice public String getPrice() { return price; } // ********** getPrice() public String getPricePriceAdds() { int adjustedPrice = 0; ResultSet ars = null; try { PreparedStatement getAdjustedPrice; String s = new String("SELECT priceadd FROM options WHERE priceadd IS NOT NULL AND optionid IN( "); for(int x = 0; x < options.size() - 1; x++) { s += Integer.parseInt(options.get(x).toString()) + ","; } s += Integer.parseInt(options.get(options.size()).toString()); // end SQL statement s += ")"; getAdjustedPrice = con.prepareStatement(s); ars = getAdjustedPrice.executeQuery(); while(ars.next()){ adjustedPrice += Integer.parseInt(ars.getString("priceadd")); } adjustedPrice += Double.parseDouble(getPrice()); con.close(); } catch (SQLException sqle) { sqle.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return Integer.toString(adjustedPrice); } // end getPrice // *********** getOptionInfo() public ResultSet getOptionInfo(String productid) throws SQLException, Exception { ResultSet rs = null; try { PreparedStatement getOptionInfo; String s = new String("SELECT * FROM options WHERE optionid = ?;"); getOptionInfo = con.prepareStatement(s); getOptionInfo.setInt(1, optionid); rs = getOptionInfo.executeQuery(); } catch (SQLException sqle) { sqle.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return rs; } // end getOptionInfo // ************ connect() public void connect() throws ClassNotFoundException, SQLException, Exception { try { // load driver Class.forName("org.gjt.mm.mysql.Driver"); // make connection con = DriverManager.getConnection("jdbc:mysql:///storecybertrailscomtest","", graphics/ccc.gif ""); } catch (ClassNotFoundException cnfe) { error = "Class not found: can't find driver"; throw new ClassNotFoundException(error); } catch (SQLException sqle) { sqle.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } //end connect //********* connect() public void disconnect() throws SQLException { try { if ( con != null ) { //close connection con.close(); } } catch (SQLException sqle) { sqle.printStackTrace(); } } // end disconnect() }

We use these same connect and disconnect methods over again in other classes. It might be a good exercise for you to offload some of this work into a database utility object as we did with the Swing query app in Chapter 12.

16.7.2 Order.java

[View full width]
 
[View full width]
package com.cybertrails.store; import java.util.*; import java.sql.*; import javax.servlet.http.*; import javax.servlet.*; public class Order { //variables private HttpSession session; private HttpServletRequest request; private Connection con; private String error; private String customerid, shipinfoid, orderid, orderproductsid; private String bName, bAddress, bCity, bState, bZipCode, bCountry, bPhone, bEmail, creditnumber, creditname, creditexpdate; private String sName, sAddress, sCity, sState, sZipCode, sCountry, sPhone, sEmail; public Order(HttpSession ses) { session = ses; } public void dbController() { try{ //Insert the customer into the database putCustomer(); //Get the customer ID of the customer //just inserted getCustomerID(); //Insert the shipping info into the database putShipInfo(); //Get the shipinfo ID for the orders table getShipInfoID(); // put the order in putOrder(); //Get the orderid getOrderID(); //Enter the order into the database putOrderProducts(); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error:" + sqle; System.err.println(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in DBController"; System.err.println(error); } } public void putCustomer() throws SQLException, Exception { bName = (String) session.getAttribute("bName"); bAddress = (String)session.getAttribute("bAddress"); bCity = (String)session.getAttribute("bCity"); bState = (String)session.getAttribute("bState"); bZipCode = (String)session.getAttribute("bZipCode"); bCountry = (String)session.getAttribute("bCountry"); bPhone = (String)session.getAttribute("bPhone"); bEmail = (String)session.getAttribute("bEmail"); creditnumber = (String)session.getAttribute("creditnumber"); creditname = (String)session.getAttribute("creditname"); creditexpdate = (String)session.getAttribute("creditexpdate"); String customerSQL; customerSQL = "INSERT into customers" + "(billname, billaddress, " + "billcity, billstate, billzipcode, billcountry, " + "billphone, billemail,creditnumber, creditname, " + "creditexpdate) VALUES ('" + bName + "', '" + bAddress + "', '" + bCity + "', '" + bState + "', " + bZipCode + ", '" + bCountry + "', '" + bPhone + "', '" + bEmail + "', '" + creditnumber + "', '" + creditname + "', '" + creditexpdate + "');"; try { Statement putOrder = con.createStatement(); putOrder.executeUpdate(customerSQL); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured"; throw new Exception(error); } } ////////////getCustomerID() public void getCustomerID() throws SQLException, Exception { ResultSet rs = null; String cidSQL; cidSQL = "SELECT max(customerid) AS customerid from customers"; try { Statement getCustomerID; getCustomerID = con.createStatement(); rs = getCustomerID.executeQuery(cidSQL); while(rs.next()) { customerid = rs.getString("customerid"); } } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in getCustomerID"; throw new Exception(error); } } ////////////// PUT SHIP INFO public void putShipInfo() throws SQLException, Exception { sName = (String)session.getAttribute("sName"); sAddress = (String)session.getAttribute("sAddress"); sCity = (String)session.getAttribute("sCity"); sState = (String)session.getAttribute("sState"); sZipCode = (String)session.getAttribute("sZipCode"); sCountry = (String)session.getAttribute("sCountry"); sPhone = (String)session.getAttribute("sPhone"); sEmail = (String)session.getAttribute("sEmail"); String shipSQL; shipSQL = "INSERT into shippinginfo" + "(shipname, shipaddress, " + "shipcity, shipstate, shipzipcode, shipcountry, " + "shipphone, shipemail) VALUES ('" + sName + "', '" + sAddress + "', '" + sCity + "', '" + sState + "', " + sZipCode + ", '" + sCountry + "', '" + sPhone + "', '" + sEmail + "');"; try { Statement putShipping = con.createStatement(); putShipping.executeUpdate(shipSQL); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in put shipping"; throw new Exception(error); } } // end putShipInfo ////////////GET SHIP INFO ID public void getShipInfoID() throws SQLException, Exception { ResultSet rs = null; String siidSQL; siidSQL = "SELECT max(shipinfoid) AS shipinfoid from shippinginfo"; try { Statement getShipInfoID; getShipInfoID = con.createStatement(); rs = getShipInfoID.executeQuery(siidSQL); while(rs.next()) { shipinfoid = rs.getString("shipinfoid"); } } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in getShpInfoID"; throw new Exception(error); } } public void putOrder() throws SQLException, Exception { // get stuff we need from session String subtotal = (String) session.getAttribute("subTotal"); String tax = (String) session.getAttribute("tax"); String shipCost = (String) session.getAttribute("shipCost"); String total = (String) session.getAttribute("total"); // order date java.util.Date orderDate = new java.util.Date(); String oSQL; oSQL = "INSERT INTO Orders (customerid, shipinfoid, subtotal, " + "shipping, tax, grandtotal, orderdate, isprocessed) " + "VALUES (" + customerid + ", " + shipinfoid + ", " + subtotal + ", " + shipCost + " , " + tax + ", " + total + ", '" + orderDate + "',0);"; try { PreparedStatement putOrder; putOrder = con.prepareStatement(oSQL); putOrder.executeUpdate(); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in putOrder"; throw new Exception(error); } } ////////////get order id public void getOrderID() throws SQLException, Exception { ResultSet rs = null; String oidSQL; oidSQL = "SELECT max(orderid) AS orderid from orders"; try { Statement getOrderID; getOrderID = con.createStatement(); rs = getOrderID.executeQuery(oidSQL); while(rs.next()) { orderid = rs.getString("orderid"); } // add to session so we can output it // on the Thankyou.jsp page session.setAttribute("orderid", orderid); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in getOrderID"; throw new Exception(error); } } //////// PUT ORDERPRODUCTS public void putOrderProducts() throws SQLException, Exception { // get the cart ArrayList currentItems = (ArrayList)session.getAttribute("currentItems"); String poSQL; String productid; String qty; double adjPrice; // loop over for(int i = 0 ; i < currentItems.size(); i++) { Item temp = (Item)currentItems.get(i); productid = temp.getProductID(); qty = temp.getQuantity(); //This is the price plus the options the user chose adjPrice = temp.getAdjustedPrice(); poSQL = "INSERT INTO OrdersProducts (orderid, productid, qty, price )" + "VALUES (" + orderid + ", " + productid + ", " + qty + ", " + adjPrice + ");"; try { PreparedStatement putOrderProducts; putOrderProducts = con.prepareStatement(poSQL); putOrderProducts.executeUpdate(); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in putOrderProducts"; throw new Exception(error); } getOrderProductsID(); putOrdersProductsOptionChoice(temp); }//end of loop over currentitems } ////////////GET OrderProductsID public void getOrderProductsID() throws SQLException, Exception { ResultSet rs = null; String opidSQL; opidSQL = "SELECT max(ordersproductsid) AS orderproductsid from ordersproducts"; try { Statement getOrderProductsID; getOrderProductsID = con.createStatement(); rs = getOrderProductsID.executeQuery(opidSQL); while(rs.next()) { orderproductsid = rs.getString("orderproductsid"); } } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in getOrdersproductsID"; throw new Exception(error); } } //////// PUT OrdersProductsOptionChoice public void putOrdersProductsOptionChoice(Item temp) throws SQLException, Exception { // get the cart ArrayList optionIDs = temp.getOptionIDs(); String opocSQL; String optionid; // loop over for(int i = 0 ; i < optionIDs.size(); i++) { optionid = (String)optionIDs.get(i); opocSQL = "INSERT INTO ordersproductoptionchoice (ordersproductsid, optionid )" + "VALUES (" + orderproductsid + ", " + optionid + ");"; try { PreparedStatement putOrdersProductsOptionChoice; putOrdersProductsOptionChoice = con.prepareStatement(opocSQL); putOrdersProductsOptionChoice.executeUpdate(); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in putOrdersProductsOptionChoice"; throw new Exception(error); } }// End of For Loop to enter all the optionID's } // ************ CONNECT public void connect() throws ClassNotFoundException, SQLException, Exception { try { // load driver Class.forName("org.gjt.mm.mysql.Driver"); // make connection con = DriverManager.getConnection("jdbc:mysql:///storecybertrailscomtest","", graphics/ccc.gif ""); } catch (ClassNotFoundException cnfe) { error = "Class not found: can't find driver"; throw new ClassNotFoundException(error); } catch (SQLException sqle) { error = sqle.getMessage(); throw new SQLException(error); } catch (Exception e) { error = "Exception: unknown"; throw new Exception(error); } } //end connect //********* DISCONNECT public void disconnect() throws SQLException { try { if ( con != null ) { //close connection con.close(); } } catch (SQLException sqle) { error = ("SQLException: can't close connection"); throw new SQLException(error); } } // end disconnect() } // eoc

   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

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