Java Reference
In-Depth Information
L ISTING 20.10
Continued
// Get category_id from the request
String title_id = request.getParameter(“title_id”);
// Get a database connection from CATALOG_POOL
ConnectionPool pool =
(ConnectionPool)context.getAttribute(“CATALOG_POOL”);
Connection con = pool.getConnection();
try {
if ( con != null ) {
Statement statement = con.createStatement();
// Get the title info. from the database
StringBuffer query = new StringBuffer(“SELECT * FROM TITLES”);
query.append(“ WHERE TITLE_ID = “ + title_id);
ResultSet rs =
statement.executeQuery(query.toString());
if ( rs.next() ) {
String id = rs.getString(“title_id”);
String desc = rs.getString(“title_name”);
float price = rs.getFloat(“price”);
// Check the HttpSession for an exist ShoppingCart
HttpSession session = request.getSession();
ShoppingCart cart =
(ShoppingCart)session.getAttribute(“cart”);
if ( cart == null ) {
// if no ShoppingCart was found create one and add
// to the HttpSession
cart = new ShoppingCart();
session.setAttribute(“cart”, cart);
}
// Add the selected item to the ShoppingCart
cart.addItem(id, desc, price, 1);
}
else {
throw new Exception(“Could not find title!”);
}
}
}
finally {
pool.releaseConnection(con);
}
}
}
Search WWH ::




Custom Search