Step 2: Add a Database Control

In this step you add a database control to the Investigate Java control. The database control provides access to a database containing bankruptcy and other information about credit applicants.

The database control you add is a pre-built control provided by WebLogic Workshop. Pre-built controls act as interfaces to common data resources, such as databases, web services, Java Message Services, and Enterprise Java Beans. Pre-built controls free you from having to design a new interface each time you want to connect to one of these kinds of resources.

The tasks in this step are:

To Add the Record Class

The Record class is a Java object that represents an individual record within a database. In particular, it represents an individual record of the BANKRUPTCIES table. In the following tasks you will create a database control file that queries the BANKRUPTCIES table and then returns a Record object containing the results of the query.

  1. Right-click the investigateJCS folder and select New-->Java Class.
  2. In the New File dialog, in the File Name field enter Record.java.
  3. Click Create.
  4. Edit the Record.java file so it appears as follows. Code to add appears in red.
  5. package investigateJCS;
    	
    public class Record 
    { 
        static final long serialVersionUID = 1L;    
    
        public String firstname; 
        public String lastname;
        public String taxID;
        public boolean currentlyBankrupt; 
    }
  6. Press Ctrl+S to save your work. Press Ctrl+F4 to close Record.java.

To Create a Database Control File

In this task you will create a database control file, called BankrupctiesDatabase.jcx. "JCX" stands for "Java Control Extension". A JCX file extends one of Workshop's pre-built control classes, in this case the com.bea.control.DatabaseControl class, which allows easy access to a database.

  1. On the Application tab, double-click InvestigateImpl.jcs to display the file in the main work area. (Throughout this tutorial make sure that you do not confuse InvestigateImpl.jcs and InvestigateTest.jws. They are easy to confuse because of their similar names.)
  2. Click the Design View tab.
  3. From the Insert menu, choose Controls-->Database.

    The Insert Control dialog appears.
  4. Enter values as shown in the following illustration:

  5. Click Create.

    A new JCX file called BankruptciesDatabase.jcx is created in the investigateJCS folder and a new database control icon appears in Design View.

To Add a Method

In this task, you will add a method to the database control file. This method will ultimately be able to query a database and return a Record object containing the results of the query.

  1. In Design View, double-click the icon of the database control.

  2. The database control file, BankruptciesDatabase.jcx, appears in Design View.

  3. Right-click the picture of the database control and select Add Method.

  4. In the space provided, replace newMethod1 with checkForBankruptcies and press Enter.



    A new method is added to the database control file.

To Associate a SQL Query with the Method

In this task you will associate a SQL query with the checkForBankruptcies method, so that when the method is invoked, it will query the BANKRUPTCIES table. The results of the query will be returned by the method as a return value.

  1. Click the blue arrow icon for the checkForBankruptcies.

  2. On the Property Editor tab, in the section labeled SQL, find the property named statement. To the far right side of the value field, click the elipses symbol.

  3. In the Edit SQL and Interface dialog, enter values as shown in the illustration. Note that the illustration does not contain selectable text. As a convenience, use the selectable text shown in red. Also: to enter text into the dialog, select the target pane and press Ctrl+V.

    Enter into the SQL pane...
    SELECT TAXID, FIRSTNAME, LASTNAME, CURRENTLYBANKRUPT FROM BANKRUPTCIES WHERE TAXID={taxID}
    Enter into the Java pane...
    public Record checkForBankruptcies(String taxID)
  4. Click Ok.
  5. Click the name of the method checkForBankruptcies.



    The source code for the checkForBankruptcies method should appear as follows.



    The checkForBankruptcies method works in the following way. (1) A client, in this case the Investigate Java control, invokes the checkForBankruptcies method by passing in an applicant's taxID as a parameter. (2) The checkForBankruptcies method then uses the taxID parameter to query the BANKRUPTCIES table with a SQL query, appearing in the @jc:sql annotation. (3) Finally, the checkForBankruptcies method automatically constructs a Record object based on the results returned from the database and returns the Record object to the client. (The checkForBankruptcies method is able to automatically construct a Record object from the results of the database query because the field names of the BANKRUPTCIES table match those of the Record class.)
  6. Press Ctrl+S to save your work and press Ctrl+F4 to close BankruptciesDatabase.jcx.

To Edit the Investigate Java Control to Incorporate the Database Control File

In this task you will modify the Investigate Java control to use the database control. You will edit the method requestCreditReport to invoke the database control method checkForBankruptcies. The data returned by the checkForBankruptcies method will be stored in the member variable m_currentApplicant.

  1. If InvestigateImpl.jcs is not displayed, then, from the Window menu, select InvestigateImpl.jcs.
  2. Click the Source View tab.
  3. Edit the requestCreditReport method to look like the following. (Delete the entire body of the requestCreditReport method and replace it with the code shown in red below.)
        public void requestCreditReport(String taxID)
        {
            /*
             * Assemble a credit-worthiness report on the applicant.
             */
            // m_currentApplicant.taxID = taxID;
            // m_currentApplicant.firstName = "unknown";
            // m_currentApplicant.lastName = "unknown";
            // m_currentApplicant.approvalLevel = "unknown";
            // m_currentApplicant.message = "There was insufficient data available to assemble a full report on this applicant.";
    
            /*
             * Send the credit-worthiness report to the client via a callback.
             */
            // callback.onCreditReportDone(m_currentApplicant);
            
            m_currentApplicant.taxID = taxID;
        
            /*
             * Retrieve data from the database and store it in the rec object.
             */
            Record rec = bankruptciesDB.checkForBankruptcies(taxID);
      
            /*
             * If the database returns substantial data, then store that data
             * in the m_currentApplicant object.
             */
            if(rec != null)
            {
                m_currentApplicant.firstName = rec.firstname;
                m_currentApplicant.lastName = rec.lastname;
                m_currentApplicant.currentlyBankrupt = rec.currentlyBankrupt;
    
                /*
                 * Send the credit report to the client via a callback.
                 */ 
                callback.onCreditReportDone(m_currentApplicant);
            }
            /*
             * If the database does not return substantial data, notify the client
             * that there is a problem.
             */
            else
            {
                m_currentApplicant.message = "No data could be found on the applicant. Please call (555) 555-5555 for assistance. ";
    
                /*
                 * Send the the error message to the client via a callback.
                 */ 
                callback.onCreditReportDone(m_currentApplicant);
            }  
        }
    
  4. Press Ctrl+S to save your work.

To Test the Web Service Using the Debugger

Next you will test your Java control using the debugger. The debugger allows you to 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.

  1. Confirm that InvestigateImpl.jcs is displayed in the main work area. If necessary, click the Source View tab.
  2. Place the cursor on the first line of code within the method requestCreditReport. (The first line of code is "m_currentApplicant.taxID = taxID;")
  3. Click the Toggle Breakpoint button on the toolbar, as shown here:

    A breakpoint is set as shown below.

  4. On the Application tab, double-click InvestigateTest.jws.
  5. Press the Start button, shown below.

  6. In the Workshop Test Browser, in the taxID field, enter the 9 digit number 222222222 and click the requestCreditReport button.

    Note: The bankruptcies database 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 Java control throughout the tutorial.
  7. Note that the execution of code halts at the breakpoint as shown below.

  8. On the Locals tab, expand the entries for this and m_currentApplicant, as shown below.

  9. Click the Step Over button on the toolbar, shown here:

    Each time you press the Step Over button, Workshop executes the current line of code and moves to the next line of code (within the current file).

  10. Click the Step Over button until the web service finishes executing. The fields of the m_currentApplicant object are modified as data is retrieved from the database. As you click the Step Over button, watch the values of m_currentApplicant change on the Locals tab of the Debug Window. (Execution will stop after a total of 8 clicks of the Step Over button.)
  11. Return to the Test Browser and click Refresh.
  12. Under Message Log, click callback.onCreditRepotDone. Notice that the database has provided three new pieces of information about the applicant: a first name, last name, and his bankruptcy status.

  13. Return to WebLogic Workshop and press Ctrl+F9 to clear the breakpoint from InvestigateImpl.jcs.
  14. Click the Stop button to close the Workshop Test Browser

Related Topics

Debugging Your Application

Database Control

Click one of the following arrows to navigate through the tutorial: