© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2 Information Tier: Database 31.3 Using the Cloudscape Database in JSP Pages 31.4 Wrap-Up Tutorial 31 – Bookstore Application: Information Tier Examining the Database and Creating Database Components
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objective In this tutorial, you will learn to: –Connect to a database. –Create SQL statements that retrieve information from a database.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Reviewing the Bookstore Application When the books.jsp page is requested Retrieve the book titles from the database Display the book titles in an HTML menu If the user selects a book title from the menu and clicks the View Information (submit) button Request the bookInformation.jsp page for the selected title When the bookInformation.jsp page is requested from books.jsp Retrieve the selected book’s information from a database for the selected title Format the retrieved information in the bookInformation.jsp page Return the result to the client browser If the user clicks the Book List link on the bookInformation.jsp page Request the books.jsp page
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Reviewing the Bookstore Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Reviewing the Bookstore Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Information Tier: Database Products table –Nine columns –productID, title, authors, copyrightYear, edition, isbn, cover, description and price
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Information Tier: Database
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages Scriptlet (scripting element) –Block of Java code in a JSP – JSP comment –
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.4 Adding the template for the JSP scriptlet. JSP commentsStart JSP scriptlet Java code inside the scriptlet End JSP scriptlet
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.5 Setting the location of the database. Specifying database location setProperty method Specifies database location Escape sequence \\
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.6 Loading the database driver. Loading database driver class
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.7 Connecting to the database. Connecting to bookstore database Connection object –Connects to the bookstore database
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.8 Creating a Statement object. Creating Statement object to execute SQL statement Statement object –Use the database connection to execute SQL statements
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.9 Getting information from the database. Executing query to get book titles executeQuery method –Retrieve information from the database
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Closing the connection to the database. Closing database connection
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Ending the JSP scriptlet and catching exceptions. out object –implicit objects –println method Displaying error message
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Running the updated books.jsp page.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Adding the template for the JSP scriptlet. Adding JSP scriptlet
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Setting the location of the database. Specifying database location
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Loading the database driver. Loading database driver class
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Creating the JDBC connection. Connecting to bookstore database
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Creating the Statement object. Creating Statement object to execute an SQL statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Creating the Statement object. request object –getParameter method Executing query to get book information
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Ending the scriptlet so that literal HTML markup can be inserted in the response to the client. Ending JSP scriptlet
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Closing the database connection. Continuing the JSP scriptlet Java code inside the scriptlet that closes the database connection and displays error message if exception occurs Ending JSP scriptlet
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Cloudscape Database in JSP Pages (Cont.) Figure Running the updated bookInformation.jsp page.
2004 Prentice Hall, Inc. All rights reserved. Outline Book List Available Books books.jsp (1 of 4)
2004 Prentice Hall, Inc. All rights reserved. Outline Select a book from the list and click the button to view 25 the selected book's information <% 32 // setup database connection 33 try 34 { 35 // specify database location 36 System.setProperty( "db2j.system.home", 37 "C:\\Examples\\Tutorial29\\Databases" ); // load Cloudscape driver 40 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" ); // connect to bookstore database 43 Connection connection = 44 DriverManager.getConnection( 45 "jdbc:db2j:bookstore" ); 46 books.jsp (2 of 4) JSP comment Start JSP scriptlet Specifying database location Loading database driver class Connecting to the bookstore database
2004 Prentice Hall, Inc. All rights reserved. Outline // obtain list of titles 48 if ( connection != null ) 49 { 50 Statement statement = 51 connection.createStatement(); ResultSet results = statement.executeQuery( 54 "SELECT title FROM Products" ); // close database connection 57 connection.close(); } // end if } // end try // catch SQLException 64 catch( SQLException exception ) 65 { 66 out.println( 67 "Exception: " + exception + " occurred." ); 68 } %> 71 books.jsp (3 of 4) Creating Statement to execute SQL query Executing query to get book titles Closing database connection Displaying error message End JSP scriptlet
2004 Prentice Hall, Inc. All rights reserved. Outline books.jsp (4 of 4)
2004 Prentice Hall, Inc. All rights reserved. Outline Book Information <% bookInformation.jsp (1 of 4) JSP comment Start JSP scriptlet
2004 Prentice Hall, Inc. All rights reserved. Outline // setup database connection 26 try 27 { 28 // specify database location 29 System.setProperty( "db2j.system.home", 30 "C:\\Examples\\Tutorial29\\Databases" ); // load Cloudscape driver 33 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" ); // obtain connection to database 36 Connection connection = DriverManager.getConnection( 37 "jdbc:db2j:bookstore" ); // obtain list of titles from database 40 if ( connection != null ) 41 { 42 // create statement 43 Statement statement = connection.createStatement(); // execute query to get book information bookInformation.jsp (2 of 4) Specifying database location Loading database driver class Connecting to the bookstore database Creating Statement to execute SQL query
2004 Prentice Hall, Inc. All rights reserved. Outline ResultSet results = statement.executeQuery( 47 "SELECT cover, title, authors, price, " + 48 "isbn, edition, copyrightYear, description " + 49 "FROM Products WHERE title = '" + 50 request.getParameter( "bookTitle" ) + "'" ); %> Authors: Price: ISBN: Edition: Copyright Year: bookInformation.jsp (3 of 4) Executing query to get book information End JSP scriptlet to begin inserting literal HTML markup
2004 Prentice Hall, Inc. All rights reserved. Outline Description: Book List <% // continue scriptlet connection.close(); // close database connection } // end if } // end try // catch SQLException 87 catch( SQLException exception ) 88 { 89 out.println( "Exception: " + exception + " occurred." ); 90 } %> bookInformation.jsp (4 of 4) Continue JSP scriptletEnd JSP scriptlet