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. |
Next: The MenuBean Class >>
More Java Articles
More By McGraw-Hill/Osborne