Step 3: Add a Database Control
In this step you add a database control to your web service. The database control provides your web service access to a database containing bankruptcy information about credit applicants. Controls, i.e., CTRL files, act as interfaces between your web service and other data resources, such as databases, other web services, Java Message Services, etc.
The tasks in this step are:
To Create a Database CTRL file
In this task you will create a database CTRL file and then add a method to this CTRL file. The method you add, called checkForBankruptcies, will query a database using a SQL query.
If you are not in Design View, click the Design View tab.
From the Add Control drop-down list, select Add a Database Control. The Add Database Control dialog appears.
Enter values as shown in the following illustration:
Click Create.
Right-click the newly created database control and select Add Method.
In the field that appears, type checkForBankruptcies and press Enter.
In Design View, right-click the arrow associated with the checkForBankruptcies method and select Edit SQL and Interface, as shown here. The EditSQL and Interface dialog appears.
Enter values as shown in the illustration below
and click OK.
Use the selectable text to cut and paste in the EditSQL
and Interface dialog.
For the SQL widow:
SELECT TAXID, FIRSTNAME, LASTNAME, CURRENTLYBANKRUPT FROM BANKRUPTCIES
WHERE TAXID={taxID}
For the Java window:
public Investigate.Applicant checkForBankruptcies(String taxID)
To Edit the Web Service Code to Incorporate the CTRL File
Next you must modify the web service's code to take advantage of the database control that has been added. You will edit the method requestCreditReportAsynch to invoke the control method checkForBankruptcies. Any information found in the database is stored in the member variable m_currentApplicant.
Click the Source View tab.
Edit the requestCreditReportAsynch method to look like the following. Remember to remove the line of code shown crossed out below.
public void requestCreditReportAsynch(String taxID) throws java.sql.SQLException { /* * Query the database via the database control */ Applicant dbApplicant = bankruptciesDB.checkForBankruptcies(taxID); /* * If the database contains data on the current applicant, * assign the retrieved values to m_currentApplicant. */ if(dbApplicant != null) { m_currentApplicant = dbApplicant; } m_currentApplicant.taxID = taxID; callback.onCreditReportDone(m_currentApplicant, null); }
To Test the Web Service Using the Debugger
Next you will test your web service using the debugger. The debugger allows you set breakpoints in your code and track how your code is running line-by-line. Setting a breakpoint in your code will cause the Java Virtual Machine to halt execution of your code immediately before the breakpoint, allowing you to step through your code beginning at that point.
If you are not in Source View, click the Source View tab.
Place the cursor on the first line of code executed within the method requestCreditReportAsynch.
Click the Toggle Breakpoint button on the toolbar, as shown here:
The breakpoint appears on the first line of code, as shown here:
Press the Start and Debug button on the toolbar, shown here:
The Test View page appears.
In the taxID
box, type the nine-digit number 222222222
and click requestCreditReportAsynch.
Note: The database you just added to your web service contains
data on 6 individuals. The taxIDs of these individuals are 123456789,
111111111, 222222222,
333333333, 444444444,
and 555555555. Use these six taxIDs to test
your web service throughout the tutorial.
The web service does not run through its routine of method calls and callbacks. To verify this, refresh the browser and note that the service has halted at the requestCreditReportAsynch.
Return to WebLogic Workshop and note that the execution of code has halted at the breakpoint shown here:
On the Locals tab of the Debug Window pane, expand the entries for this and m_currentApplicant, as shown here:
Note: The Debug Window pane gives you information about the current values of the variables within your web service, as well as current position in the call stack.
Click the Step Into button on the toolbar, shown here:
Each time you press the Step Into button, a new line of code within the requestCreditReportAsynch method is executed. The properties of the m_currentApplicant are filled with values as they are retrieved from the database. As you click the Step Into button, watch the values of m_currentApplicant change on the Locals tab of the Debug Window.
Continue clicking the Step Into button until the web service finishes executing.
Return to the browser window that displays the test page and refresh the browser.
Under Message Log, click callback.onCreditRepotDone. The response sent back to the client application appears, as shown here:
You have completed adding a database control and testing your web service with the debugger. In the next step of the tutorial you will add a service control to your web service.
Database Control: Using a Database from Your Web Service
Click one of the following arrows to navigate through the tutorial.