Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2."— Presentation transcript:

1 © Copyright 1992-2004 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

2 © Copyright 1992-2004 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.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 31.1 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

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 31.1 Reviewing the Bookstore Application (Cont.)

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 31.1 Reviewing the Bookstore Application (Cont.)

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 31.2 Information Tier: Database Products table –Nine columns –productID, title, authors, copyrightYear, edition, isbn, cover, description and price

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 31.2 Information Tier: Database

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 31.3 Using the Cloudscape Database in JSP Pages Scriptlet (scripting element) –Block of Java code in a JSP – JSP comment –

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 31.3 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

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 31.3 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 \\

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.6 Loading the database driver. Loading database driver class

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 31.3 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

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 31.3 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

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 31.3 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

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.10 Closing the connection to the database. Closing database connection

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.11 Ending the JSP scriptlet and catching exceptions. out object –implicit objects –println method Displaying error message

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.12 Running the updated books.jsp page.

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.13 Adding the template for the JSP scriptlet. Adding JSP scriptlet

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.14 Setting the location of the database. Specifying database location

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.15 Loading the database driver. Loading database driver class

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.16 Creating the JDBC connection. Connecting to bookstore database

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.17 Creating the Statement object. Creating Statement object to execute an SQL statement

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.18 Creating the Statement object. request object –getParameter method Executing query to get book information

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.19 Ending the scriptlet so that literal HTML markup can be inserted in the response to the client. Ending JSP scriptlet

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.20 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

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 31.3 Using the Cloudscape Database in JSP Pages (Cont.) Figure 31.21 Running the updated bookInformation.jsp page.

27  2004 Prentice Hall, Inc. All rights reserved. Outline 27 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Book List 15 16 17 18 19 Available Books 20 21 22 23 books.jsp (1 of 4)

28  2004 Prentice Hall, Inc. All rights reserved. Outline 28 24 Select a book from the list and click the button to view 25 the selected book's information 26 27 28 29 30 31 <% 32 // setup database connection 33 try 34 { 35 // specify database location 36 System.setProperty( "db2j.system.home", 37 "C:\\Examples\\Tutorial29\\Databases" ); 38 39 // load Cloudscape driver 40 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" ); 41 42 // 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

29  2004 Prentice Hall, Inc. All rights reserved. Outline 29 47 // obtain list of titles 48 if ( connection != null ) 49 { 50 Statement statement = 51 connection.createStatement(); 52 53 ResultSet results = statement.executeQuery( 54 "SELECT title FROM Products" ); 55 56 // close database connection 57 connection.close(); 58 59 } // end if 60 61 } // end try 62 63 // catch SQLException 64 catch( SQLException exception ) 65 { 66 out.println( 67 "Exception: " + exception + " occurred." ); 68 } 69 70 %> 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

30  2004 Prentice Hall, Inc. All rights reserved. Outline 30 72 73 74 75 76 77 78 79 books.jsp (4 of 4)

31  2004 Prentice Hall, Inc. All rights reserved. Outline 31 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Book Information 15 16 17 18 19 20 21 22 23 24 <% bookInformation.jsp (1 of 4) JSP comment Start JSP scriptlet

32  2004 Prentice Hall, Inc. All rights reserved. Outline 32 25 // setup database connection 26 try 27 { 28 // specify database location 29 System.setProperty( "db2j.system.home", 30 "C:\\Examples\\Tutorial29\\Databases" ); 31 32 // load Cloudscape driver 33 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" ); 34 35 // obtain connection to database 36 Connection connection = DriverManager.getConnection( 37 "jdbc:db2j:bookstore" ); 38 39 // obtain list of titles from database 40 if ( connection != null ) 41 { 42 // create statement 43 Statement statement = connection.createStatement(); 44 45 // 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

33  2004 Prentice Hall, Inc. All rights reserved. Outline 33 46 ResultSet results = statement.executeQuery( 47 "SELECT cover, title, authors, price, " + 48 "isbn, edition, copyrightYear, description " + 49 "FROM Products WHERE title = '" + 50 request.getParameter( "bookTitle" ) + "'" ); 51 52 %> 53 54 55 56 57 58 Authors: 59 60 61 Price: 62 63 64 ISBN: 65 66 67 Edition: 68 69 70 Copyright Year: bookInformation.jsp (3 of 4) Executing query to get book information End JSP scriptlet to begin inserting literal HTML markup

34  2004 Prentice Hall, Inc. All rights reserved. Outline 34 71 72 73 Description: 74 75 76 Book List 77 78 <% // continue scriptlet 79 80 connection.close(); // close database connection 81 82 } // end if 83 84 } // end try 85 86 // catch SQLException 87 catch( SQLException exception ) 88 { 89 out.println( "Exception: " + exception + " occurred." ); 90 } 91 92 %> 93 94 95 bookInformation.jsp (4 of 4) Continue JSP scriptletEnd JSP scriptlet


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2."

Similar presentations


Ads by Google