WebSphere Studio Application Developer Version 5 Programming Guide part 32 ppt

10 134 0
WebSphere Studio Application Developer Version 5 Programming Guide part 32 ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

284 WebSphere Studio Application Developer Version 5 Programming Guide Two classes are generated:  GetCustomerBeanRow—Provides access to one row of the result set.  GetCustomerBean—Executes the stored procedure and provides a method to retrieve an array of GetCustomerBeanRow objects. Using the JavaBean The generated JavaBean, GetCustomerBean, can be used in a servlet or JSP to execute the stored procedure and access the result set. A simple JSP to execute the JavaBean is shown in Figure 9-16. The code is available in: \sg246957\sampcode\dev-proc\RunGetCustomerBean.jsp Figure 9-16 Simple JSP to execute the JavaBean with the stored procedure The GetCustomerBean is instantiated using a <useBean> tag. The stored procedure is executed and the first row of the result set (there is only one) is retrieved and displayed. Note that the customer ID is passed as a constant and no error checking is performed. In a real application the code would be more complex. <BODY> <H1>JSP -> JavaBean -> Stored Procedure</H1> <jsp:useBean id="getCustomer" class="itso.storedproc.bean.GetCustomerBean"></jsp:useBean> <% getCustomer.execute( new Integer(106) ); %> <% itso.storedproc.bean.GetCustomerBeanRow row = getCustomer.getRows()[0]; %> <TABLE border="1"> <TR><TH align="left">ID</TH> <TD><%= row.getCUSTOMER_CUSTOMERID() %></TD></TR> <TR><TH align="left">Title</TH> <TD><%= row.getCUSTOMER_TITLE() %></TD></TR> <TR><TH align="left">Firstname</TH> <TD><%= row.getCUSTOMER_FIRSTNAME() %></TD></TR> </TABLE> </BODY> Tip: Restart the enterprise application in the server after making changes to the Web application deployment information. Chapter 9. Developing applications with stored procedures 285 Using the JSP tag library Stored procedures can also be executed through the jspsql tag library. See “Accessing a database using JSP taglib” on page 262 for a description. To make the tag library available for the Web application, we have to import the JAR file into the lib directory and add the tag library to the deployment descriptor:  Select the ItsoProGuideStroedProcWeb\Web Content\WEB-INF\lib folder and Import (context). Select File system , then locate the directory: <wsadhome>\wstools\eclipse\plugins\com.ibm.etools.dbjars_5.0.1\jars  Select only the jspsql.jar file and click Finish .  Open the deployment descriptors (web.xml). On the References page, select the JSP tag libraries tab. Click Add and select the jspsql tag library. Save the deployment descriptor. Figure 9-17 shows a sample JSP that uses the tag library to execute the stored procedure. The code is available in: \sg246957\sampcode\dev-proc\RunStoredProcedure.jsp Figure 9-17 JSP with tag library to execute stored procedure <%@taglib uri="jspsql" prefix="dab"%> <BODY> <H1>JSP with Stored Procedure Call</H1> <dab:dataSourceSpec id="DSConnect" scope="page" dataSource='jdbc/ejbbank' /> <dab:procedureCall id="getCust" connectionSpec="DSConnect" scope="request"> <dab:sql> { CALL ITSO.GETCUSTOMER(:customerID) } </dab:sql> <dab:parameter parmName="customerID" type="INTEGER" mode="in" value="104" /> </dab:procedureCall> <TABLE border="1"> <TR><TH align="left">ID</TH> <TD><dab:getColumn name="getCust" index="1" /></TD></TR> <TR><TH align="left">Title</TH> <TD><dab:getColumn name="getCust" index="2" /></TD></TR> <TR><TH align="left">Firstname</TH> <TD><dab:getColumn name="getCust" index="3" /></TD></TR> </TABLE></BODY> 286 WebSphere Studio Application Developer Version 5 Programming Guide Generate JavaBean Web pages You can use the Create Web Pages from a JavaBean wizard to build the input and output pages to test the JavaBean that calls the stored procedure. This wizard is described in “Creating Web pages from a JavaBean” on page 237, therefore we only provide short instructions here to run through the wizard:  Select New -> Other -> Web -> JavaBean Web Pages .  Select /ItsoProGuideStoredProcWeb/Web Content as destination and itso.storedproc.web as package.  Select itso.storedproc.bean.GetCustomerBean as the bean. Click Introspect if necessary.  Select the rows property (the result of the stored procedure) and the execute method (to run the stored procedure).  Select Create a new front controller .  Tailor the input page with: – Title: Execute Stored Procedure – Prompt: Enter a customer ID:  Tailor the result page with: – Title: Stored Procedure Results – Label for rows property: Customer – Expand rows, select the six properties (all except class), and set short labels for all the properties (Title, Lastname, UserID, Password, ID, Firstname)  Leave GetCustomerBean as prefix and click Finish to generate the code. You get a servlet, an HTML input page, and a result JSP: GetCustomerBeanController.java GetCustomerBeanInputForm.html GetCustomerBeanResultsForm.jsp To test the generated application, restart the enterprise application (select the ItsoServer and Restart Project -> ItsoProGuide ) or restart the server. Select the GetCustomerBeanInputForm.html file and Run on Server . Enter a customer ID and click Submit . A sample run is shown in Figure 9-18. Chapter 9. Developing applications with stored procedures 287 Figure 9-18 Running the generated Web application Creating a stored procedure with multiple statements Here we want to create a stored procedure that transfers money between two accounts. Therefore, we have to execute two SQL statements in sequence, one to withdraw money from one account, and one to deposit money into another account. To create this stored procedure:  In the Data perspective, select the Stored Procedures folder and New -> Stored Procedure .  Enter transferMoney as name.  On the definition page, select None for result sets and SP_JAVA_HDR.FRAGMENT for the header fragment. Click Change to enter the SQL statements. 288 WebSphere Studio Application Developer Version 5 Programming Guide  In the SQL statement dialog (Figure 9-19) enter the first SQL statement as: UPDATE ITSO.ACCOUNT SET BALANCE = (BALANCE - :amount) WHERE ACCID = :account1 AND BALANCE > :amount  Click Add to get space for another SQL statement and enter: UPDATE ITSO.ACCOUNT SET BALANCE = (BALANCE + :amount) WHERE ACCID = :account2  Click OK and then click Next . Figure 9-19 Stored procedure with two statements  In the Parameters dialog, one parameter named whichQuery is predefined. The generated code assumes that you want to run one of the two statements. We will have to run both. We will take care of this in the generated code.  Define these parameters (Figure 9-20): –In: account1, VARCHAR, 8 –In: account2, VARCHAR 8 –In: amount, DECIMAL, 8.2 –Out: success, INTEGER Chapter 9. Developing applications with stored procedures 289 Figure 9-20 Parameters for the stored procedure  You can skip the rest of the dialog and click Finish . Edit the generated code The generated Java class TransferMoney uses the whichQuery parameter to decide which statement to execute. We have to change the code to execute both statements and to set the return value. Our changes are as follows:  Remove the whichQuery parameter.  Remove the switch and case statements.  Create variables for both SQL statements.  Run both statements in sequence.  Set the result as the sum of the changed rows (a result of 2 would be a success). The changed method is shown in Figure 9-21. 290 WebSphere Studio Application Developer Version 5 Programming Guide Figure 9-21 Updated stored procedure with two SQL statements public static void transferMoney ( int whichQuery, String account1, String account2, java.math.BigDecimal amount, int[] success ) throws SQLException, Exception { // Get connection to the database Connection con = DriverManager.getConnection("jdbc:default:connection"); PreparedStatement stmt = null; PreparedStatement stmt2 = null; int updateCount = 0; int updateCount2 = 0; boolean bFlag; String sql, sql2; switch (whichQuery) { case 0: sql = "UPDATE ITSO.ACCOUNT" + " SET BALANCE = (BALANCE - ?) " + " WHERE ACCID = ? " + " AND BALANCE > ?"; stmt = con.prepareStatement( sql ); stmt.setBigDecimal( 1, amount ); stmt.setString( 2, account1 ); stmt.setBigDecimal( 3, amount ); bFlag = stmt.execute(); rs1 = stmt.getResultSet(); updateCount = stmt.executeUpdate(); break; case 1: sql2 = "UPDATE ITSO.ACCOUNT" + " SET BALANCE = (BALANCE + ?) " + " WHERE ACCID = ?"; stmt2 = con.prepareStatement( sql2 ); stmt2.setBigDecimal( 1, amount ); stmt2.setString( 2, account2 ); bFlag = stmt.execute(); rs1 = stmt.getResultSet(); updateCount2 = stmt2.executeUpdate(); break; default: sql = "SELECT PROCSCHEMA, PROCNAME FROM SYSCAT.PROCEDURES"; stmt = con.prepareStatement( sql ); } updateCount = stmt.executeUpdate(); // Set return parameter success[0] = updateCount + updateCount2; } Chapter 9. Developing applications with stored procedures 291 Build and execute Build the stored procedure by selecting Build from the context menu. Test the procedure by selecting Run . Enter 106-6001, 106-6002, and 100.00 as parameters. The DB Output view (Figure 9-22) shows the result value in the Parameters tab. Figure 9-22 Stored procedure run Using a JSP We provide the RunTranfer.jsp to execute the transferMoney stored procedure. The core code is shown in Figure 9-23. Figure 9-23 JSP to execute the transfer stored procedure Tip: The database is not updated by default. Select the transferMoney stored procedure and Run Settings (context). On the Options tab you can select Commit changes to the database . <dab:dataSourceSpec id="DSConnect" scope="page" dataSource='jdbc/ejbbank' /> <dab:procedureCall id="transfer" connectionSpec="DSConnect" scope="request"> <dab:sql> { CALL ITSO.TRANSFERMONEY (:account1, :account2, :amount, :success) } </dab:sql> <dab:parameter parmName="account1" type="VARCHAR" mode="in" value="106-6002" /> <dab:parameter parmName="account2" type="VARCHAR" mode="in" value="106-6001" /> <dab:parameter parmName="amount" type="DECIMAL" mode="in" value="100.00" /> <dab:parameter parmName="success" type="INTEGER" mode="out" /> </dab:procedureCall> Amount: <dab:getParameter name="transfer" parmName="amount" /><br> Result: <dab:getParameter name="transfer" parmName="success" /> 292 WebSphere Studio Application Developer Version 5 Programming Guide Summary In this chapter we developed two stored procedures, one with a result set and one that updates the database. We also showed how stored procedures can be invoked through JavaBeans and JSPs. © Copyright IBM Corp. 2003. All rights reserved. 293 Chapter 10. Developing Struts applications In this chapter we introduce you to the Jakarta Struts framework and Application Developer’s support for building Struts-based Web applications. The chapter describes how to:  Create a Web project with Struts support  Create a Web diagram and lay out the components of the application  Implement form beans, JSPs, and actions  Use the Struts configuration file editor  Test the application 10 . 284 WebSphere Studio Application Developer Version 5 Programming Guide Two classes are generated:  GetCustomerBeanRow—Provides access. index="3" /></TD></TR> </TABLE></BODY> 286 WebSphere Studio Application Developer Version 5 Programming Guide Generate JavaBean Web pages You can use the Create Web Pages. header fragment. Click Change to enter the SQL statements. 288 WebSphere Studio Application Developer Version 5 Programming Guide  In the SQL statement dialog (Figure 9-19) enter the first

Ngày đăng: 03/07/2014, 20:20

Tài liệu cùng người dùng

Tài liệu liên quan