} The function getSearchResults() executes the $searchStmtquery and

February 2nd, 2007

} The function getSearchResults() executes the $searchStmtquery and returns the result as an array of Book_Item objects: function getSearchResults($searchStmt) { Get the database connection: $functionResult = getDBConnection(); if ($functionResult->returnValue == null) { return $functionResult; } $link = $functionResult->returnValue; Execute the $searchStmt SQL query: if (!($result = mysql_query($searchStmt, $link))) { return new Function_Result(”Internal Error: Could not execute sql query”, null); } $searchResults = null; while (($row = mysql_fetch_array($result, MYSQL_NUM))) { For each retrieved row, add a Book_Item object to $searchResultsarray: $searchResults[] = new Book_Item($row[0], $row[1], $row[2], $row[3], $row[4]); } mysql_free_result($result); Return the $searchResultsarray: return new Function_Result(null, $searchResults); } } ?> The Music_Shop Class The script MusicShop.php contains the class definition of Music_Shop class: Hint: If you are looking for high quality webhost to host and run your jsp application check Vision web hosting jsp services

Return Book_Item object: return new Function_Result(null, $item); }

February 2nd, 2007

Return Book_Item object: return new Function_Result(null, $item); } } The function search() returns Book_Item objects which contain $searchText in authoror title column: function search($searchText) { SELECT statement retrieves the book items which contain $searchText in author or title column: $searchStmt = “SELECT itemNo, itemType, price, title, author FROM BookShop WHERE author LIKE ‘%” . $searchText . “%’ OR title LIKE ‘% ” . $searchText . “%’” ; Call getSearchResults() function to execute the $searchStmt SQL query: $funcResult = $this->getSearchResults($searchStmt); Return the search result: return $funcResult->returnValue; } The function searchByTitle() returns Book_Item objects which contain $searchText in title column: function searchByTitle($searchText) { $searchStmt = “SELECT itemNo, itemType, price, title, author FROM BookShop WHERE title LIKE ‘% ” . $searchText . “%’” ; $funcResult = $this->getSearchResults($searchStmt); return $funcResult->returnValue; } The function searchByAuthor() returns the book items which contain $searchText in the author column: function searchByAuthor($searchText) { $searchStmt = “SELECT itemNo, itemType, price, title, author FROM BookShop WHERE author LIKE ‘% ” . $searchText . “%’” ; $funcResult = $this->getSearchResults($searchStmt); return $funcResult->returnValue; Page 566

Hint: This post is supported by Gama php5 hosting services

For each retrieved row, add a Book_Item object

February 1st, 2007

For each retrieved row, add a Book_Item object to the $bookShopContent array: $bookShopContent[] = new Book_Item($row[0], $row[1], $row[2], $row[3], $row[4]); } mysql_free_result($result); Return $bookShopContent: return new Function_Result(null, $bookShopContent); } The function getItem() returns the Book_Item object corresponding to item number $itemNo: function getItem($itemNo) { // Get DB Connection $functionResult = getDBConnection(); if ($functionResult->returnValue == null) { return $functionResult; } $link = $functionResult->returnValue; SELECT query to retrieve book information for item number $itemNo: $bookShopSelectQuery = “SELECT itemNo, itemType, price, title, author FROM BookShop WHERE itemNo=’” . $itemNo . “‘”; Execute the query: if (!($result = mysql_query($bookShopSelectQuery, $link))) { return new Function_Result(”Internal Error: Could not execute sql query “, null); } Fetch the row from the result of the query: $row = mysql_fetch_array($result, MYSQL_NUM); if ($row == null) { return new Function_Result(null, null); } else { Create a Book_Item object for the retrieved row: $item = new Book_Item($row[0], $row[1], $row[2], $row[3], $row[4]); Page 565

Hint: This post is supported by Gama web hosting php mysql provider

The function getArtist() returns the value of member

February 1st, 2007

The function getArtist() returns the value of member variable artist: function getArtist() { return $this->artist; } } ?> The Book_Shop Class The script BookShop.phpcontains the definition of Book_Shop class: returnValue == null) { return $functionResult; } $link = $functionResult->returnValue; SELECT query to retrieve all the books from the database: $bookShopSelectQuery = “SELECT itemNo, itemType, price, title, author FROM BookShop”; Execute the SQL query: if (!($result = mysql_query($bookShopSelectQuery, $link))) { return new Function_Result(”Internal Error: Could not execute sql query “, null); } $bookShopContent = null; while (($row = mysql_fetch_array($result, MYSQL_NUM))) { Page 564
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services

function getTitle() { return $this->title; } The function

February 1st, 2007

function getTitle() { return $this->title; } The function getAuthor() returns the value of member variable author: function getAuthor() { return $this->author; } } ?> The Music_Item Class The script MusicItem.php contains the class definition of Music_Item class: Item($itemNo, $itemType, $price); Set the value of member variables $title and $artist: $this->title = $title; $this->artist = $artist; } The function getTitle() returns the value of member variable title: function getTitle() { return $this->title; } Page 563
Note: If you are looking for inexpensive but high quality provider to host and run your serlvet application check Astra servlet hosting services

{ return $this->itemNo; } The function getItemType() returns

February 1st, 2007

{ return $this->itemNo; } The function getItemType() returns the value of member variable itemType: function getItemType() { return $this->itemType; } The function getPrice() returns the value of member variable price: function getPrice() { return $this->price; } } ?> The Book_Item Class The script BookItem.php contains the class definition of Book_Item class: Item($itemNo, $itemType, $price); Set the value of member variables $title and $author: $this->title = $title; $this->author = $author; } The function getTitle() returns the value of member variable title: Page 562

Hint: If you are looking for very good and affordable webspace to host and run your java hosting application check Sandzak.com java web hosting provider

} ?> Data and Application Logic Layer One

February 1st, 2007

} ?> Data and Application Logic Layer One of the implicit design goals of this application is to separate application logic from the presentation layer (refer to Chapter 15 for more on this). In this application we will achieve this goal by implementing the data access and application logic in separate classes that are used by the presentation layer. The following is the list of classes implementing the data access and application logic: . Item: An abstract class encapsulating the common properties of different types of items. . Book_Item: This class models a book item. . Music_Item: This class models a music item. . Book_Shop: This class contains logic to get/search book items from BookShop table. . Music_Shop: This class contains logic to get/search music items from MusicShop table. . Shopping_Cart: This class models the user’s shopping cart. It implements the logic to add/remove items from the cart. . Transaction: The classes stores the details of user transaction. . Credit_Card: The class stores the credit card details. . Shipping_Address: The class stores the shipping address. . User: This class models the user. It stores all the user attributes and implements the checkout logic. It delegates all the save operations to User _Storage. . User_Storage: This class implements the save operation for the User object. . UserFactory: Implements functions for creating a new user and for loading users from the database. The Item Class The script Item.php contains the class definition of Item class: itemNo = $itemNo; $this->itemType = $itemType; $this->price = $price; } The function getItemNo() returns the value of member variable itemNo: function getItemNo() Page 561

Hint: This post is supported by Gama php5 hosting services

// Execute the query if (!($result = mysql_query($deleteStmt,

February 1st, 2007

// Execute the query if (!($result = mysql_query($deleteStmt, $link))) { return false; } return mysql_affected_rows($link); } The function gc() deletes the rows corresponding to the sessions which have not been accessed for the last $maxlifetime seconds: function gc($maxlifetime) { global $dbHostName, $dbUserName, $dbPassword, $dbName; // Get a persistent Connection if (!($link = mysql_pconnect($dbHostName, $dbUserName, $dbPassword))) { return false; } // select mysql database if (!mysql_select_db($dbName)) { return false; } Prepare the statement for deleting all the rows for which the value of lastAccessed column of the Session table plus $maxlifetime is greater than the current time. Whenever a row is accessed through a SELECT, UPDATE, or REPLACE SQL statement, the value of lastAccessed column is set to the current time: // DELETE Statement $deleteStmt = “DELETE FROM Session WHERE CURRENT_TIMESTAMP < (lastAccessed + ". $maxlifetime . ")"; // Execute the query if (!($result = mysql_query($deleteStmt, $link))) { return false; } return mysql_affected_rows($link); } The function setSessionHandlers(), sets the session handlers: function setSessionHandlers() { session_set_save_handler("open", "close", "read", "write", "destroy", "gc"); Page 560

Hint: If you are looking for high quality and reliable webspace provider to host and run your jsp hosting application check Sandzak jsp web hosting provider

// Get a persistent Connection if (!($link =

February 1st, 2007

// Get a persistent Connection if (!($link = mysql_pconnect($dbHostName, $dbUserName, $dbPassword))) { return false; } // select mysql database if (!mysql_select_db($dbName)) { return false; } Prepare the REPLACE statement for storing the session data. If there exists a row with $id as the value of its id column, then the REPLACE statement deletes the previous row and inserts the new row with the new values, otherwise it inserts a new row: // REPLACE Statement $replaceStmt = “REPLACE INTO Session(id, data) VALUES (’$id’, ‘$data’)”; Execute the query: // Execute the query if (!($result = mysql_query($replaceStmt, $link))) { return false; } return mysql_affected_rows($link); } The function destroy() deletes the row corresponding to the session whose identifier is $id: function destroy($id) { global $dbHostName, $dbUserName, $dbPassword, $dbName; // Get a persistent Connection if (!($link = mysql_pconnect($dbHostName, $dbUserName, $dbPassword))) { return false; } // select mysql database if (!mysql_select_db($dbName)) { return false; } Prepare the DELETE statement for deleting the row with $id as the value of id column: // DELETE Statement $deleteStmt = “DELETE FROM Session WHERE id = ‘$id’”; Page 559
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra j2ee hosting services

if (!($link = mysql_pconnect($dbHostName, $dbUserName, $dbPassword))) { return

February 1st, 2007

if (!($link = mysql_pconnect($dbHostName, $dbUserName, $dbPassword))) { return null; } Select the MySQL database which contains the Session table: // select mysql database if (!mysql_select_db($dbName)) { return null; } Make the SELECT statement for retrieving the row containing data of the session: // SELECT Statement $selectStmt = “SELECT data FROM Session WHERE id = ‘” . $id . “‘”; Execute the MySQL query: // Execute the query if (!($result = mysql_query($selectStmt, $link))) { return null; } Fetch the row from the result of the query. If there was no row corresponding to the session then return null: if (($row = mysql_fetch_array($result, MYSQL_NUM))) { $data = $row[0]; } else { $data = null; } Free the MySQL result object: mysql_free_result($result); Return the session data: return $data; } The function write() stores the session data in the Sessiontable. The argument $id is the session identifier and the argument $data is the session data: function write($id, $data) { global $dbHostName, $dbUserName, $dbPassword, $dbName; Get a persistent database connection and select the MySQL database: Page 558
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services