Java
  Home arrow Java arrow Page 4 - Online Store Application
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Online Store Application
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2005-03-16

    Table of Contents:
  • Online Store Application
  • Understanding the Page Control Flow
  • Structuring the Database Tables
  • The DatabaseUtil Class
  • The MenuBean Class
  • The ShoppingItemBean Class
  • Registering the Beans in the Application Configuration File
  • Adding the ActionListener
  • Creating the JSP Pages
  • The search.jsp Page
  • The shoppingCart.jsp Page

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Online Store Application - The DatabaseUtil Class


    (Page 4 of 11 )

    The DatabaseUtil class is shown in Listing 14-3.

    Listing 14-3  The DatabaseUtil Class

      package buydirect;
      import java.sql.*; import java.util.ArrayList;
      import java.util.Collection;
      import java.util.Iterator;
     
    public class DatabaseUtil {
        String dbUrl = "jdbc:odbc:buydirect";
        String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
        public void setDbUrl(String dbUrl) {
          this.dbUrl = dbUrl;
      }
      public void setJdbcDriver(String jdbcDriver) {      
      
          this.jdbcDriver = jdbcDriver;
      }
     
    public ArrayList getCategories() {
        ArrayList categories = new ArrayList();
        String sql = "SELECT Id, Description FROM Categories";      try {
          Class.forName(jdbcDriver);
          Connection connection = DriverManager.getConnection(dbUrl);
          Statement statement = connection.createStatement(); 
          ResultSet resultSet = statement.executeQuery(sql);  
          while (resultSet.next()) {
            String id = resultSet.getString(1);
            Category category = new Category(id, description);          categories.add(category);
          }
          resultSet.close();
          statement.close();
          connection.close();
        
    }
        catch (ClassNotFoundException e) {
        }
        catch (SQLException e) {
        }
        catch (Exception e) {
        }
        return categories;
      }
     
    public ProductBean getProductDetails(String productId) 
        {
    String sql =
           "SELECT ProductId, Name, Description, Price FROM Products" +
           " WHERE ProductId=" + productId;
        ProductBean result = null;
        try {
          Class.forName(jdbcDriver);
          Connection connection = DriverManager.getConnection(dbUrl);
          Statement statement = connection.createStatement(); 
          ResultSet resultSet = statement.executeQuery(sql);
          if (resultSet.next()) {
            String id = resultSet.getString(1);
            String name = resultSet.getString(2);
            String description = resultSet.getString(3);
            float price = resultSet.getFloat(4);
            result = new ProductBean(id, name, description, price);
           }
           resultSet
    .close();
           statement.close();
           connection.close();
         }
         catch (ClassNotFoundException e) {
         }
         catch (SQLException e) {
         }
         catch (Exception e) {
         }
         return result;
       } 
       public ArrayList searchProducts(String searchKey) {
         ArrayList products = new ArrayList();
         String sql = "SELECT ProductId, Name, Price FROM Products" +
          
    " WHERE Name LIKE '%" + searchKey + "%'" +
           " OR Description LIKE '%" + searchKey + "%'";
         try {
           Class.forName(jdbcDriver);
           Connection connection = DriverManager.getConnection(dbUrl);
           Statement statement = connection.createStatement();        ResultSet resultSet = statement.executeQuery(sql);        while (resultSet.next()) {
            
    String id = resultSet.getString(1);
             String name = resultSet.getString(2);
             float price = resultSet.getFloat(3);
             ProductBean product = new ProductBean(id, name, price);
             products.add(product);
           }
           resultSet.close();
           statement.close();
           connection.close();
         }
         catch (ClassNotFoundException e) {
         }
         catch (SQLException e) {
           System.out.println(e.toString());
         }
         catch (Exception e) {
         }
         return products;
         }
        
    public ArrayList getProductsByCategory(String categoryId) {
         ArrayList products = new ArrayList();
         String sql = "SELECT ProductId, Name, Price FROM Products" +
           " WHERE CategoryId=" + categoryId; 
           System.out.println("getProductsBYCategory. categoryId = " +
             categoryId);
         try {
           Class.forName(jdbcDriver);
           Connection connection = DriverManager.getConnection(dbUrl);
           Statement statement = connection.createStatement();        ResultSet resultSet = statement.executeQuery(sql);        while (resultSet.next()) {
            
    String id = resultSet.getString(1);
             String name = resultSet.getString(2);
             float price = resultSet.getFloat(3);
             ProductBean product = new ProductBean(id, name, price);
             products.add(product);
          
    }
           resultSet.close();
           statement.close();
           connection.close();
         }
         catch (ClassNotFoundException e) {
         }
         catch (SQLException e) {
           System.out.println(e.toString());
         }
         catch (Exception e) {
         }
         return products;
       
    }
       public synchronized void insertOrder(OrderBean order,  
         ShoppingCartBean shoppingCart) {
         long orderId = System.currentTimeMillis();
         ArrayList products = new ArrayList();
         String contactName = order.getContactName();
         String deliveryAddress = order.getDeliveryAddress();      String creditCardName = order.getCreditCardName();   
         String creditCardNumber = order.getCreditCardNumber();      String creditCardExpiryDate = order.getCreditCardExpiryDate();
         String sql = "INSERT INTO Orders" +
           " (OrderId, ContactName, DeliveryAddress, CCName," +        " CCNumber, CCExpiryDate)" +
           " VALUES" +
           " (" + orderId + "," +
           "'" + contactName + "'," +
           "'" + deliveryAddress + "'," +
           "'" + creditCardName + "'," +
           "'" + creditCardNumber + "'," +
           "'" + creditCardExpiryDate + "')";
         try {
           Class.forName(jdbcDriver);
           Connection connection = DriverManager.getConnection(dbUrl);
           Statement statement = connection.createStatement();        statement.executeUpdate(sql);
           Iterator shoppingItems = 
             shoppingCart.getShoppingItems().iterator();
           while (shoppingItems.hasNext()) {
             ShoppingItemBean item =
               (ShoppingItemBean) shoppingItems.next();
             String productId = item.getProductId();
             int quantity = item.getQuantity();
             float price = item.getPrice();
             sql = "INSERT INTO OrderDetails" +
               " (OrderId, ProductId, Quantity, Price)" +
               " VALUES" +
               " (" + orderId + "," +
               productId + "," +
               quantity + "," +
               price + ")";
            
    statement.executeUpdate(sql);
           }
           statement.close();
           connection.close();
         }
         catch (ClassNotFoundException e) {
         }
         catch (SQLException e) {
           System.out.println(e.toString());
         }
         catch (Exception e) {
         }
      
    }
      }


    Representing the Model Objects and Registering Beans

    A number of classes represent model objects in this application. Also, several of the JavaBeans must be registered in the application configuration file.

    Defining Classes for the Application

    The following classes are used in this application:

    • Category
    • MenuBean
    • ProductBean
    • ProductDetailsBean 
    • ShoppingItemBean
    • ShoppingCartBean
    • OrderBean
    • BrowseBean

    Each of these classes is discussed in the following sections.

    The Category Class

    The Category class, shown in Listing 14-4, contains information about a product category. There are two properties in this class: id and description.

    Listing 14-4  The Category Class

    package buydirect;
    public class Category {
      private String id;
      private String description;
      public Category(String id, String description) {
        this.id = id;
        this.description = description;
      }
      public String getId() {
        return id;
      }
      public void setId(String id) {
        this.id = id;
      }
      public String getDescription() {
       
    return description;
      }
      public void setDescription(String description) {
        this.description = description;
      }
    }


    This article is excerpted from JavaServer Faces Programming by Budi Kurniawan (McGraw-Hill, 2003; ISBN 0072229837). Check it out at your favorite bookstore today. Buy this book now.

    More Java Articles
    More By McGraw-Hill/Osborne


       · I go thru the online application, and I'm new to this topic, can anyone recreate...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway