FileDocCategorySizeDatePackage
ProductManager.javaAPI DocExample6022Wed Apr 05 20:04:14 BST 2006com.oreilly.ajax

ProductManager

public class ProductManager extends Object

Fields Summary
Constructors Summary
Methods Summary
public static java.lang.StringgetJSONProducts()

    return getProducts();
  
public static ProductgetProduct(java.lang.String productName)

    Product product = new Product();
    Connection con = DatabaseConnector.getConnection();
    String sqlString = "";
    try
    {
      sqlString = "SELECT * FROM PRODUCTS WHERE PRODUCT_NAME='" + productName
          + "';";
      Statement select = con.createStatement();
      ResultSet result = select.executeQuery(sqlString);

      if (result.next())
      { // process results one row at a time

        product.setProductName(result.getString("PRODUCT_NAME"));
        product.setDescription(result.getString("DESCRIPTION"));
        product.setFilename(result.getString("FILENAME"));
        product.setPrice(result.getFloat("PRICE"));
        product.setProductId(result.getInt("PRODUCT_ID"));

      }

    }
    catch (Exception e)
    {
      System.out.println("exception caught getting Product" + sqlString + " "
          + e.getMessage());
    }
    finally
    {
      if (con != null)
      {
        try
        {
          con.close();
        }
        catch (SQLException e)
        {
        }
      }
    }

    return product;
  
public static ProductgetProductById(java.lang.String productID)

    Product product = new Product();
    Connection con = DatabaseConnector.getConnection();
    String sqlString = "";
    try
    {
      sqlString = "SELECT * FROM PRODUCTS WHERE PRODUCT_ID='" + productID
          + "';";
      Statement select = con.createStatement();
      ResultSet result = select.executeQuery(sqlString);

      if (result.next())
      { // process results one row at a time

        product.setProductName(result.getString("PRODUCT_NAME"));
        product.setDescription(result.getString("DESCRIPTION"));
        product.setFilename(result.getString("FILENAME"));
        product.setPrice(result.getFloat("PRICE"));
        product.setProductId(result.getInt("PRODUCT_ID"));

      }

    }
    catch (Exception e)
    {
      System.out.println("exception caught getting Product" + sqlString + " "
          + e.getMessage());
    }
    finally
    {
      if (con != null)
      {
        try
        {
          con.close();
        }
        catch (SQLException e)
        {
        }
      }
    }
    return product;
  
private static java.lang.StringgetProducts()

    Product product = new Product();
    String title = "products";
    Connection con = DatabaseConnector.getConnection();
    StringBuffer jsonString = new StringBuffer("\r\n{\"" + title + "\": [");
    String sqlString = "";

    try
    {
      sqlString = "SELECT * FROM PRODUCTS;";
      Statement select = con.createStatement();
      ResultSet result = select.executeQuery(sqlString);
      while (result.next())
      { // process results one row at a time

        product.setProductName(result.getString("PRODUCT_NAME"));
        product.setDescription(result.getString("DESCRIPTION"));
        product.setFilename(result.getString("FILENAME"));
        product.setPrice(result.getFloat("PRICE"));
        product.setProductId(result.getInt("PRODUCT_ID"));
        jsonString.append("\r\n{\"description\":\"" + product.getDescription()
            + "\",\"name\":\"" + product.getProductName()
            + "\",\"filename\":\"" + product.getFilename() + "\", \"price\":\""
            + product.getPrice() + "\"},");

      }
      int lastCharIndex = jsonString.length();
      jsonString.deleteCharAt(lastCharIndex - 1);
      jsonString.append("\r\n]}");
    }
    catch (Exception e)
    {
      System.out.println("exception caught getting Product" + sqlString + " "
          + e.getMessage());
    }
    finally
    {
      if (con != null)
      {
        try
        {
          con.close();
        }
        catch (SQLException e)
        {
        }
      }
    }
    return jsonString.toString();
  
public static java.lang.StringgetProductsView()

    Connection con = DatabaseConnector.getConnection();
    String sqlString = "";
    StringBuffer htmlStringBuffer = new StringBuffer(
        "<table class=\"Product\">");
    htmlStringBuffer.append("\n<tr><th class=\"Product\">Product Name</th>");
    htmlStringBuffer.append("\n<th class=\"Product\">Description</th>");
    htmlStringBuffer.append("\n<th class=\"Product\">Filename</th>");
    htmlStringBuffer.append("\n<th class=\"Product\">Price </th></tr>");
    try
    {
      sqlString = "select * from PRODUCTS";
      Statement select = con.createStatement();
      ResultSet result = select.executeQuery(sqlString);
      Date tempDate = null;
      while (result.next())
      { // process results one row at a time
        htmlStringBuffer.append("\n<tr>");
        htmlStringBuffer.append("\n<td class=\"Product\">"
            + result.getString("PRODUCT_NAME") + "</td>");
        htmlStringBuffer.append("\n<td class=\"Product\">"
            + result.getString("DESCRIPTION") + "</td>");
        htmlStringBuffer.append("\n<td class=\"Product\">"
            + result.getString("FILENAME") + "</td>");
        htmlStringBuffer.append("\n<td class=\"Product\">"
            + result.getString("PRICE") + "</td>");

        htmlStringBuffer.append("</tr>");
      }
    }
    catch (Exception e)
    {
      System.out.println("exception caught getting PRODUCTS" + sqlString + " "
          + e.getMessage());
    }
    finally
    {
      if (con != null)
      {
        try
        {
          con.close();
        }
        catch (SQLException e)
        {
        }
      }
    }
    return htmlStringBuffer.toString();