Home arrow Java arrow Page 3 - Online Store Application

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
  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

Online Store Application - Structuring the Database Tables
(Page 3 of 11 )

The BuyDirect application uses four tables: Categories, Products, Orders, and OrderDetails. The Categories table stores product categories, as shown in Table 14-1.

The Products table stores the details of each product sold in the online store. Table 14-2 lists the column names and data types in this table.

The Orders table, shown in Table 14-3, holds order information, including delivery addresses, credit card details, and contact names. There is a unique identifier for each order to link all shopping items in that order. These shopping items are stored in the OrderDetails table, shown in Table 14-4.

Column NameData Type

Table 14-1 The Categories Table in the Database

Column NameData Type

Table 14-2 The Products Table in the Database

Column NameData Type

Table 14-3 The Orders Table in the Database

Column NameData Type

Table 14-4 The OrderDetails Table in the Database

Connecting to the Database

Java Database Connectivity (JDBC) is the technology for accessing a database and manipulating its data in a Java application. JDBC has two parts: the JDBC Core Application Programming Interface (API) and the JDBC Optional Package API. The most recent version of JDBC is 3.0 and it is included in Java 2, Standard Edition (J2SE) version 1.4.1 and later. (At the time of the writing of this book, JDBC version 4.0 was being developed.)

Basically, to access a database, you simply connect to the database server. Then you can send a SQL query to retrieve data from a table, update records, insert new records, delete data, and so on.

You need to perform four steps to access a database with JDBC:

  • Load the JDBC database driver.
  • Create a connection.
  • Create a statement.
  • Create a result set, if you expect the database server to send back some data.


JDBC drivers normally come in a .jar file. To use the driver from a Web application, you need to copy this jar file to the WEB-INF/lib directory under your application directory. If you are using the JDBC-ODBC bridge, however, you do not need to install the driver, because it’s already included in your Java Development Kit (JDK).

Setting Up an ODBC Data Source Name

You need to set up an Open Database Connectivity (ODBC) Data Source Name (DSN) on your computer if you want to use the Microsoft Access database provided on the CD-ROM that accompanies this book. To set up an ODBC DSN, follow these steps:

  1. Open the ODBC Data Source Administration dialog  
    box from the Control Panel.
  2. Click the System DSN tab.
  3. Click the Add button.
  4. Select a driver name from the list of ODBC drivers.
  5. Enter the information requested in the Setup dialog 
    box that appears. For accessing the buydirect.mdb 
    Microsoft Access database, the name for the DSN 
    must be buydirect.
  6. Click OK to close the dialog box

The DatabaseUtil class, explained in the next section, provides methods for the rest of the application. For production, you may also want to use connection pooling to make your application more scalable.

Using the DatabaseUtil Class

As part of our BuyDirect application, various business objects need to access the database. A utility class called DatabaseUtil class is provided for this purpose. This class provides the methods used by other classes to access and manipulate the data in the database.

DatabaseUtil Class Methods

The DatabaseUtil class methods work with the Category, ProductBean, OrderBean, ShoppingCartBean, and ShoppingItemBean classes, which are described in the “Defining Classes for the Application” section later in this chapter. The DatabaseUtil class provides the following methods:

  • getCategories This method accepts no argument and returns an ArrayList containing all of the categories from the Categories table. Each category is represented by the Category class. The signature of the getCategories method is as follows:

        public ArrayList getCategories()

  • searchProducts This method returns an ArrayList containing all of the products found for a given search key. A product is represented by the ProductBean class. Here is the signature of the searchProducts method:

        public ArrayList searchProducts(String searchKey)

  • getProductsByCategory This method returns an ArrayList containing all of the products belonging to the specified category identifier passed in as the argument to this method. A product is represented by the ProductBean class. The signature of the getProductsByCategory method is as follows:

        public ArrayList getProductsByCategory(String  

  • getProductDetails This method accepts a product identifier and returns the details of the product encapsulated in an instance of ProductBean. The signature of the getProductDetails method is as follows:

        public ProductDetailsBean getProductDetails(String

  • insertOrder You use this method to insert a purchase order. This method accepts two arguments: an order and a shopping cart. An order, represented by the OrderBean class, contains the delivery and payment information (the contact name, delivery address, and credit card details). The shopping cart, represented by the ShoppingCartBean class, contains all the shopping items. A shopping item is represented by the ShoppingItemBean class. Here is the signature of the insertOrder method:

     public synchronized void insertOrder (OrderBean       
       shoppingCartBean shoppingCart) {

In addition, the DatabaseUtil class has two methods for initializing values required for accessing the database: setJdbcDriver and setDbUrl.

The DatabaseUtil Instance

The application needs only one instance of this class. To avoid creating multiple instances of this class, the application instantiates the class at startup and stores it in ServletContext, in the attribute named DATABASE_UTIL. The instance can then be retrieved by using the following code:

FacesContext facesContext = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext)

  facesContext.getExternalContext().getContext(); DatabaseUtil dbUtil = (DatabaseUtil)


To store the DatabaseUtil instance at application startup, you use the servlet context listener called AppContextListener, shown in Listing 14-2.

Listing 14-2  The AppContextListener Class

 package buydirect;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 public class AppContextListener implements ServletContextListener {
  public void contextInitialized(ServletContextEvent event) {
    DatabaseUtil dbUtil = new DatabaseUtil(); 
    ServletContext servletContext = event.getServletContext();
    String jdbcDriver =
    String dbUrl = servletContext.getInitParameter("dbUrl");
    servletContext.setAttribute("DATABASE_UTIL", dbUtil);
public void contextDestroyed(ServletContextEvent event) {
    ServletContext servletContext = event.getServletContext();

The DatabaseUtil instance needs the dbUrl and jdbcDriver information provided as context initial parameters in the deployment descriptor (web.xml file), as follows:


You need to change the values of the dbUrl and jdbcDriver parameters to the values of your database server’s database URL and JDBC driver.

This means that you can use any database server from any vendor, as long as you provide the correct JDBC driver and URL to the database. Note that you must also copy the JDBC driver to the WEB-INF/lib directory.

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 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 

Developer Shed Affiliates


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