254 WebSphere Studio Application Developer Version 5 Programming Guide Figure 8-7 Create Database Web Pages wizard: design result form Here you can change the heading of the columns and page properties. If you do not want to show one or more of the fields retrieved from the query, you can deselect them in the top left pane. Typically, you would only select a subset of the fields for the master table. To change the labels, select the item in the top left pane and make your changes in the bottom left pane. Typically, you would change the headings of the table to short descriptive names. The next page shows the default form for showing details of a selected row in the details view page (Figure 8-8). You can make the same type of changes here as on the master table view page to improve the look of the page. Chapter 8. Developing Web applications with database access 255 Figure 8-8 Create Database Web Pages wizard: design detail form Finally, you can specify the default prefix for all generated objects (Figure 8-9). Clicking Finish generates the Java classes and HTML/JSP pages. Figure 8-9 Create database Web pages wizard: select prefix 256 WebSphere Studio Application Developer Version 5 Programming Guide Defining a data source in the server Before we can test the application, we have to set up the data source in the server. To use a data source, you have to configure the server to recognize it. For our application, we have to register a data source for the EJBBANK database. These steps assume that a WebSphere v5.0 Test Environment server has already been created, as explained in “Creating a server for testing” on page 224. Open the Server perspective and edit the server configuration of the ItsoServer: Go to the Data source tab. Under Server Settings, select the Default DB2 JDBC Provider from the JDBC provider list and click Add next to the data source list. The Create a Data Source dialog is displayed (Figure 8-10). Figure 8-10 Create a Data Source dialog Select DB2 JDBC provider and make sure Version 5.0 data source is selected, then click Next . In the Modify Data Source window, as shown in Figure 8-11, enter EJBBANK as the name and jdbc/ejbbank as the JNDI name. This name has to match the name used in your application. Chapter 8. Developing Web applications with database access 257 Deselect Use this data source in container managed persistence (CMP) . For now we do not have any EJBs. Figure 8-11 Modify a Data Source Click Next to continue to the next page of the wizard, as shown in Figure 8-12. On this page, modify the databaseName field to be EJBBANK. Tip: The data source name and the JNDI name can be anything. We recommend that you use the database name as the data source name and jdbc/databasename as the JNDI name. See “Installing DB2 UDB” on page 786 for instructions on how to enable JDBC 2.0, which is required for data source support. required for EJBs 258 WebSphere Studio Application Developer Version 5 Programming Guide Figure 8-12 Create a Data Source - Specify database name Select databaseName in the Resource Properties list, then enter EJBBANK in the Value field. This is the only required property. Click Finish and the data source is defined. The Data source page of the server configuration is shown in Figure 8-13. Note that you have to select a driver in the JDBC provider list to see the data sources defined for that driver. Chapter 8. Developing Web applications with database access 259 Figure 8-13 Server configuration with data source The data source is now defined, so press Ctrl-S to save the server configuration. If the server was running, it has to be restarted before running the application that uses the data source. Testing the database application To test the generated database application, start the ItsoServer, select the generated HTML input form (ListCreditsInputForm.html in this example), and select Run on Server from its context menu. A sample run is shown in Figure 8-14. 260 WebSphere Studio Application Developer Version 5 Programming Guide Figure 8-14 Sample database application run Accessing a database using DB Beans In this section, we explain how to use the DB Beans package to access a database. The DB Beans classes can be found in the com.ibm.db.beans package. Chapter 8. Developing Web applications with database access 261 To access the DB Beans from Application Developer, the following JAR file must be available in the Web Content\WEB-INF\lib folder of the folder of the project: <wsadhome>\wstools\eclipse\plugins\com.ibm.etools.dbjars_5.0.1\jars\ dbbeans.jar To import a JAR file into the lib directory of a project: Select the lib folder and Import (context). Select File system , then navigate to the dbbeans.jar file and import the file. Select the project and Properties (context). Select Java Build Path and on the Libraries page click Add JARs and select the dbbeans.jar file. The documentation for the classes in the package can be found in: <wsadhome>\wstools\eclipse\plugins\com.ibm.etools.dbjars_5.0.1\jars\ dbbeans_javadoc.zip Creating a JSP using DB Beans After you have imported the package you can use the DB Beans classes by using <jsp:useBean> to create the bean and then using scriptlets to execute methods on it. We begin by creating a new JSP called TestDBBeans.jsp. For more information on creating a JSP, see “Working with JSPs” on page 210 A simple example of a JSP that executes an SQL statement using the DB Beans classes is shown in Figure 8-15. In this example we use two of the DB Beans classes: DBConnectionSpec, which handles the database connection, and DBSelect, which wraps an SQL SELECT statement. There is a small number of classes in the DB Beans package, and they are straightforward to use. Note: The dbbeans.jar file is already in the ItsoProGuideDataBaseWeb project after using the DB Web Pages wizard. Note: This code was created using Page Designer by visually inserting beans and scriptlets. To test the JSP, select Run on Server from its context menu. 262 WebSphere Studio Application Developer Version 5 Programming Guide Figure 8-15 JSP with DB Beans example The next section describes the JSP tags that have been built on top of the beans to make it even easier to provide database access functionality to your Web application. Accessing a database using JSP taglib Application Developer provides an alternative to using the DB Beans classes described above. If you prefer, you can instead use a set of JSP tags built on top of these classes. It is also possible to mix direct calls to DB Beans and JSP tags. If you decide to use the JSP tags, you should be aware that there are some restrictions compared to using the beans directly: For any of the JSP SQL actions that require a connection to the database, a connection is opened when the tag is encountered, and closed after the tag has been processed. Two actions cannot be performed within the same transaction scope, or even using the same JDBC connection. The only <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <META name="GENERATOR" content="IBM WebSphere Studio"> <TITLE>TestDBBeans.jsp</TITLE> </HEAD> <BODY> <H2>Number of rows in CUSTOMER table</H2> <jsp:useBean id="Connect" class="com.ibm.db.beans.DBConnectionSpec"/> <jsp:useBean id="SelectStatement" class="com.ibm.db.beans.DBSelect"/> <% Connect.setDriverName("COM.ibm.db2.jdbc.app.DB2Driver"); Connect.setUrl("jdbc:db2:EJBBANK"); SelectStatement.setConnectionSpec(Connect); SelectStatement.setCommand("SELECT * FROM ITSO.CUSTOMER"); SelectStatement.execute(); out.println("Row count is: " + SelectStatement.getRowCount()); %> </BODY> </HTML> Note: The code referred to in this section can be generated using the Database Web Pages wizard and selecting the IBM Database Access Tag Library - Select Statement model. Refer to “Generate Web pages from SQL queries” on page 249. Chapter 8. Developing Web applications with database access 263 exception to this is through the <x:batch> action. Actions inside the body of the <x:batch> do share the same connection, and optionally, the same transaction. Using the DB Beans directly, you have complete control over when a database connection is opened and closed. You also have complete control over transaction scopes, with the ability to turn AutoCommit on or off and to do explicit commits or rollbacks. Some of the methods and properties of the DBSelect and DBProcedureCall beans for handling large result sets are not offered in the JSP SQL actions: – These methods and properties allow you to limit the number of rows maintained in memory at any one time and to specify how many rows to fetch at once when getting additional rows. – This limitation is necessary because of the above limitation that the database connection is closed after each JSP SQL action is processed. If only a subset of the rows is initially fetched into memory, and then the connection is closed, there is no way to later fetch the remaining rows. – The JSP SQL actions do provide some support for large result sets via the maxRows attribute of the <x:select> and <x:procedureCall> actions. This attribute simply limits the number of rows that are fetched in any one result set. –The lockRows property of the DBSelect and DBProcedureCall bean is not offered via the JSP SQL actions. This property causes a database lock to be kept on a row in the result set while it is the current row. For a Web application, it is not likely that you would want to maintain such a lock across user interactions which could span an arbitrary amount of time. – Because of the first limitation above, that the database connection is closed after each JSP SQL action is processed, it is not possible for us to maintain such a lock when you use the JSP SQL tags. When row locking is not used, either with the JSP SQL tags or with direct use of the DB Beans, optimistic locking is still used to prevent you from updating a row if someone else updates it between the time that you read it and the time that you attempt to update it. – A greater variety of methods for moving between rows and between result sets is available through direct use of the DB Beans than through the JSP SQL actions. To use the JSP database tags, you have to import the following two JAR files to the Java build path into the WEB-INF\lib folder of your project: <wsadhome>\wstools\eclipse\plugins\com.ibm.etools.dbjars_5.0.1\jars\jspsql.jar <wsadhome>\wstools\eclipse\plugins\com.ibm.etools.dbjars_5.0.1\jars\dbbeans.jar . Web pages wizard: select prefix 256 WebSphere Studio Application Developer Version 5 Programming Guide Defining a data source in the server Before we can test the application, we have to set up. 254 WebSphere Studio Application Developer Version 5 Programming Guide Figure 8-7 Create Database Web Pages wizard: design result. which is required for data source support. required for EJBs 258 WebSphere Studio Application Developer Version 5 Programming Guide Figure 8-12 Create a Data Source - Specify database name Select