Home arrow Java arrow Page 4 - Online Store Application
JAVA

Online Store Application


If you want to build an online store application using Java, look no further. This article begins with an overview of the application, and then discusses the applicatin development process. It is taken from chapter 14 of JavaServer Faces Programming by Budi Kurniawan (McGraw-Hill, 2003; ISBN 0072229837).

Author Info:
By: McGraw-Hill/Osborne
Rating: 5 stars5 stars5 stars5 stars5 stars / 54
March 16, 2005
TABLE OF CONTENTS:
  1. · Online Store Application
  2. · Understanding the Page Control Flow
  3. · Structuring the Database Tables
  4. · The DatabaseUtil Class
  5. · The MenuBean Class
  6. · The ShoppingItemBean Class
  7. · Registering the Beans in the Application Configuration File
  8. · Adding the ActionListener
  9. · Creating the JSP Pages
  10. · The search.jsp Page
  11. · The shoppingCart.jsp Page

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials