1. Trang chủ
  2. » Công Nghệ Thông Tin

WebSphere Studio Application Developer Version 5 Programming Guide part 20 pptx

10 211 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 229,42 KB

Nội dung

164 WebSphere Studio Application Developer Version 5 Programming Guide Create the schema and tables Select the ITSOTEST.sql file and Run on Database Server . The Run Script dialog opens (Figure 6-26). Select the statements that you want to execute. Figure 6-26 Run DDL script Click Next . On the next page, set the commit option, for example, Commit changes only upon success (Figure 6-27). Figure 6-27 Commit options for DDL script Click Next . On the connection page, create a connection to the ITSOTEST database. The dialog is the same as Figure 6-5 on page 146. Chapter 6. Developing database applications 165 Click Finish and the DDL statements are executed against the database. Using a DB2 Command Window you can verify that the objects are created: db2 connect to itsotest db2 select * from itso.customer db2 select * from itso.custaddress This completes our look at creating database objects. Creating SQL statements There are two alternative ways of creating an SQL statement in Application Developer:  SQL Statement Wizard—The SQL Statement Wizard is a guided dialog through a number of panels, with an SQL statement as the result.  SQL Query Builder—The SQL Query Builder is an editor for an advanced user. Both tools can be used to build an SQL statement. After using the SQL Statement Wizard, you can use the SQL Query Builder to updates the SQL statement. For our example we are developing a SELECT statement against the sample EJBBANK database. We would like to see a list of all credit transactions where the last name of the customer contains the letter “o”. We like to see the transaction ID, account ID, and the first and last name of the customer, as well as the transaction amount. Basically we construct this SQL statement: SELECT t.TRANSID, c.FIRSTNAME, c.LASTNAME, t.ACCID, t.TRANSAMT FROM CUSTOMER c, CUSTACCT a, TRANSRECORD t WHERE c.CUSTOMERID = a.CUSTOMERID AND a.ACCID = t.ACCID AND t.TRANSTYPE = 'C' AND c.LASTNAME LIKE :lastname Using the SQL Statement Wizard In this section we create an SQL statement using the wizard. To create some order in the project, select the ItsoProGuideDatabase project and New -> Folder (Navigator view, context). Enter sql as the folder name. 166 WebSphere Studio Application Developer Version 5 Programming Guide Select the ItsoProGuideDatabase project and File -> New -> Other . Select Data and SQL Statement in the New dialog and click Next to open the SQL Statement Wizard (Figure 6-28). Figure 6-28 SQL Statement Wizard: specify statement information On the first page, you select the type of statement you want to create and say that you want to use the wizard: Be guided through creating an SQL statement . There are two ways to specify the database model. You can either use an existing one or import a new one. In this case we already have the database model imported into the Workbench, so we select Use existing database model . Click the Browse button to locate the EJBBANK model in the Workbench and enter the name of the SQL statement. Selecting tables and columns On the second page of the SQL Statement Wizard you build your SQL statement by selecting tables, columns and adding joins and conditions. First we identify the tables that should be included in the query (Figure 6-29). Chapter 6. Developing database applications 167 Figure 6-29 SQL Statement Wizard: add tables You select the tables in the left pane and use the > button to include them. For our example we have the CUSTACCT, CUSTOMER, and TRANSRECORD tables. On the Columns tab you select the columns from these tables that should be included in the query (Figure 6-30). Figure 6-30 SQL Statement Wizard: add columns 168 WebSphere Studio Application Developer Version 5 Programming Guide Select FIRSTNAME and LASTNAME from the CUSTOMER table and TRANSID, ACCID and TRANSAMT from the TRANSRECORD table and move them across to the right pane. Order the output columns using the Move Up and Move Down buttons. Defining a table join Next you have to specify the join columns between the three tables on the Join tab. This is done by selecting the column from one table and dragging it to the corresponding column of the other table. In our case we link CUSTOMER.CUSTOMERID to CUSTACCT.CUSTOMERID and CUSTACCT.ACCID to TRANSRECORD.ACCID. When the joins are complete, connection symbols are displayed (Figure 6-31). Figure 6-31 SQL Statement Wizard: add joins Defining the conditions for the WHERE clause The Conditions tab is used to define the restrictions on the SELECT statement. Each condition is added to the WHERE clause (Figure 6-32). Select the Column, Operator, Value, And/Or, using the drop-down menu (visible after you click in the field). Enter the value by typing in the field. Tip: You can rearrange the tables by dragging them on the pane. You can enlarge a table by dragging the sides. You can also select the columns in this dialog step, or make changes to the selection from the previous step. Chapter 6. Developing database applications 169 Figure 6-32 SQL Statement Wizard: add conditions Using a variable In a real-life situation you might not want to hardcode that the last name contains the letter o, but instead leave it as a host variable. Therefore, do not enter '%o%' in the Value column, rather enter a variable as :lastname. This is especially useful later in “Accessing databases from a Web application” on page 248. On the next two tabs you can enter information regarding grouping (GROUP BY) and sorting of rows (ORDER BY). Once you have finished building the statement you can click Next to see the generated SQL statement (Figure 6-33). If you want, you can edit the statement directly. When you are finished editing, you can click Parse to validate that the SQL statement is correct. Tip: If you have to enter more than one condition, you must put in the AND or the OR element before the next row in the table becomes editable. 170 WebSphere Studio Application Developer Version 5 Programming Guide Figure 6-33 SQL Statement Wizard: generated SQL statement Executing an SQL statement To test the SQL statement, you click Execute and then Execute again in the next window. You are prompted for the host variable. Enter '%o%' and click Finish . The statement is executed and the results are displayed (Figure 6-34). Figure 6-34 SQL Statement Wizard: test SQL statement Host variable prompt Chapter 6. Developing database applications 171 Select Close to close the Execute SQL Statement window, then select Finish to save the SQL Statement. The SQL statement is opened in the SQL Query Builder editor. Close the editor. The SQL statement appears as EJBBANK_CreditListing.sqx in the Navigator view, and as CreditListing in the Data Definition view (under EJBBANK -> Statements ). Using SQL Query Builder The other way of creating SQL statements in Application Developer is to use the SQL Query Builder. This tool supports all the options of the SQL Statement Wizard, with the addition of WITH and FULLSELECT. In this section we will describe how to use the SQL Query Builder to build a similar SELECT statement as we did using the SQL Wizard. We develop a SELECT statement against the EJBBANK database. We would like to select credit or debit transactions where the customer’s last name is a variable. This time we want to display most of the columns of the CUSTOMER and TRANSRECORD tables. To start the SQL Query Builder, expand the database folder in the Data Definition view. Select the Statements folder and New -> Select Statement . A dialog to enter the name of the statement is displayed. Enter ListCredits and click OK . The SQL Query Builder editor is displayed (Figure 6-35). Figure 6-35 SQL Query Builder: create SELECT statement 172 WebSphere Studio Application Developer Version 5 Programming Guide To define your query, go through the following steps:  First we must add the tables that are involved in the query. In our example these are CUSTOMER, CUSTACCT, and TRANSRECORD. To add them, simply drag them from the Navigator or Data Definition view and drop them in the middle pane of the SQL Query Builder screen. The result is shown in Figure 6-36. As you can see, the tables have been added to the SELECT statement in the top pane. Figure 6-36 SQL Query Builder: adding tables  Next, select the columns from each table. To select a column, check the box next to its name. For the CUSTOMER table, select all columns except ADDRESS. For the TRANSRECORD table, select all columns. Do not select any columns of the CUSTACCT table (they are duplicates anyway). As you select the columns, the SELECT statement is updated in the top pane and the columns are added in the bottom pane.  Next, join the tables together. To join the tables, select the CUSTOMERID column in the CUSTOMER table and drag it across to the corresponding column in the CUSTACCT table. Next, select the ACCID column in the CUSTACCT table and drag it across to the corresponding column in the TRANSRECORD table. A link join join Chapter 6. Developing database applications 173 symbol is shown between the tables, and the SELECT statement is updated with the corresponding WHERE clauses.  Finally, we want to add the two conditions (TRANSTYPE = :type and LASTNAME LIKE :lastname). Use the Conditions tab in the bottom pane to add the conditions using the drop-down menus, or type them directly into the SQL statement and the Conditions tab is updated (Figure 6-37). Figure 6-37 SQL Query Builder: adding conditions Save the statement. You are prompted for the host variables; just click Cancel to dismiss the dialog. Join Conditions . Builder editor is displayed (Figure 6- 35) . Figure 6- 35 SQL Query Builder: create SELECT statement 172 WebSphere Studio Application Developer Version 5 Programming Guide To define your query, go through. select the ItsoProGuideDatabase project and New -> Folder (Navigator view, context). Enter sql as the folder name. 166 WebSphere Studio Application Developer Version 5 Programming Guide Select. (Figure 6-30). Figure 6-30 SQL Statement Wizard: add columns 168 WebSphere Studio Application Developer Version 5 Programming Guide Select FIRSTNAME and LASTNAME from the CUSTOMER table and TRANSID,

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