This folder contains the CatalogBean class, which takes users through the products in the store, allowing them to view categories, subcategories , and products.
/* * CatalogBean.java */ package com.cybertrails.store.beans; import java.sql.*; import java.util.*; public class CatalogBean { String error; Connection con; public CatalogBean() {} //************GET CATEGORIES public ResultSet getCategories() throws SQLException, Exception { ResultSet rs = null; try { Statement getCategories; String s = new String("SELECT catid, catname FROM categories;"); getCategories = con.createStatement(); rs = getCategories.executeQuery(s); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error:"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured while executing query: getCategories"; throw new Exception(error); } return rs; } // end getCategories //********** GET SUB CATS public ResultSet getSubCategories(String catid) throws SQLException, Exception { ResultSet rs = null; try { PreparedStatement getSubCategories; String s = new String("SELECT DISTINCT pc.subcatid, pc.catid, sc.subcatid, sc.subcatname " + "FROM productscategories pc " + "INNER JOIN subcategories sc " + "ON pc.subcatid = sc.subcatid " + "WHERE catid = ?;"); getSubCategories = con.prepareStatement(s); // like referencing #url.catid# in CF getSubCategories.setInt(1, Integer.parseInt(catid)); rs = getSubCategories.executeQuery(); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error:"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured while executing getsubCategories query"; throw new Exception(error); } return rs; } // end getSubCategories // g*********** GET PRODUCTS public ResultSet getProducts(String subcatid) throws SQLException, Exception { ResultSet rs = null; try { PreparedStatement getProducts; String s = new String("SELECT pc.catid, p.productid, p.name, p.price, p.shortdescription, p.smimage " + "FROM productscategories pc " + "INNER JOIN products p " + "ON pc.productid = p.productid " + "WHERE subcatid = ?;"); getProducts = con.prepareStatement(s); getProducts.setInt(1, Integer.parseInt(subcatid)); rs = getProducts.executeQuery(); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in query: getProducts"; throw new Exception(error); } return rs; } // end getProducts // *********** GET ONE PRODUCT public ResultSet getOneProduct(String productid) throws SQLException, Exception { ResultSet rs = null; try { PreparedStatement getOneProduct; String s = new String("SELECT * FROM products WHERE productid = ?;"); getOneProduct = con.prepareStatement(s); getOneProduct.setInt(1, Integer.parseInt(productid)); rs = getOneProduct.executeQuery(); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in getOneProduct"; throw new Exception(error); } return rs; } // end getOneProduct // *********** GET PRODUCT OPTIONS public ResultSet getProductOptions(String productid) throws SQLException, Exception { ResultSet rs = null; try { PreparedStatement getProductOptions; String s = new String("SELECT pos.productid, pos.optionsetid, os.optionsetname, o.optionid, o.optionname, o.priceadd " + "FROM productsoptionsets pos " + "LEFT JOIN optionsets os " + "ON pos.optionsetid = os.optionsetid " + "LEFT JOIN options o " + "ON o.optionsetid = os.optionsetid " + "WHERE pos.productid = ?;"); getProductOptions = con.prepareStatement(s); getProductOptions.setInt(1, Integer.parseInt(productid)); rs = getProductOptions.executeQuery(); } catch (SQLException sqle) { sqle.printStackTrace(); error = "SQL Error"; throw new SQLException(error); } catch (Exception e) { e.printStackTrace(); error = "An unknown exception occured in getProductOptions"; throw new Exception(error); } return rs; } // end getProductOptions // ************ 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","",""); } 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() } // eof
Top |