Develop the Codes to Call Our Web Service Project

Một phần của tài liệu practical database aprogramming with java (Trang 700 - 704)

JAVA RUNTIME OBJECTS METHOD

9.12 BUILD A WINDOWS - BASED WEB CLIENT PROJECT

9.12.2 Develop the Codes to Call Our Web Service Project

9.12.2.1 Build the Codes to Call the UpdateFaculty() Operation

Open our Windows - based client project WinClientSQL and double click on our main class FacultyFrame.java to open it. Click on the Design button to open the graphic user interface. In this client project, we want to use the Update button in this form as a trigger

c09.indd 827

c09.indd 827 7/20/2011 11:12:51 AM7/20/2011 11:12:51 AM

www.traintelco.com

to start the faculty data updating action. Therefore, double click on the Update button to open its event method cmdUpdateActionPerformed() , and enter the codes that are shown in Figure 9.56 into this method.

Let ’ s have a closer look at this piece of codes to see how it works.

A. First, a new ArrayList instance al is created and initialized. This variable is used to pick up and reserve the input - updated faculty data array.

B. Then, the ArrayList instance al is cleaned up by calling the clear() method to make sure that this object is clean before the updated parameters can be added into this instance.

C. The add() method is used to pick up and add six pieces of updated faculty information into this new ArrayList instance al . These six pieces of updated faculty information are entered by the user and stored in six text fi elds in this FacultyFrame window form. The

toString() method is used to convert each piece of these updated faculty information obtained using the getText() method that returns an Object data type to a String. The last or the seventh parameter is the original faculty name stored in the ComboName combo box. The index is necessary since it is used to indicate the position of each parameter in this ArrayList. One point to be noted is the order of adding these six text fi elds, which must be identical with order of columns in our Faculty table.

Now let ’ s add the codes that are related to calling the UpdateFaculty() operation in our Web service and created automatically by the NetBeans IDE by dragging this opera- tion node into this method. Perform the following operations to complete this code creation process:

1. Expand the Web Service References node in our client project WinClientSQL and all subnodes under this node until the WebServiceSQLPort .

2. Drag the UpdateFaculty operation under this node and place it into our opened method

cmdUpdateActionPerformed() , exactly under the codes we created in Figure 9.56 . 3. A piece of codes shown in Figure 9.57 has been automatically created and added into this

method.

It is unnecessary to explain the function of this piece of codes line by line since all of coding lines have been illustrated by the built - in comments.

Figure 9.56. The newly added codes for the cmdUpdateActionPerformed() method.

private void cmdInsertActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:

ArrayList al = new ArrayList();

al.clear();

al.add(0, NameField.getText().toString());

al.add(1, OfficeField.getText().toString());

al.add(2, PhoneField.getText().toString());

al.add(3, CollegeField.getText().toString());

al.add(4, TitleField.getText().toString());

al.add(5, EmailField.getText().toString());

al.add(6, ComboName.getSelectedItem());

………

A B C

Now let ’ s do some modifi cations to this piece of codes and add some codes to meet our data updating requirements. Enter the codes that are shown in Figure 9.58 into this method. The modifi ed codes have been highlighted in bold.

Let ’ s have a closer look at this piece of codes to see how it works.

A. The Web service operation UpdateFaculty() is executed with the ArrayList instance al , which has been fi lled with six pieces of updated faculty information as the argument of

Figure 9.57. The automatically created codes by NetBeans IDE.

………

try { // Call Web Service Operation

org.ws.sql.WebServiceSQLService service = new org.ws.sql.WebServiceSQLService();

org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();

// TODO initialize WS operation arguments here java.util.List<java.lang.Object> fdata = null;

// TODO process result here

java.lang.Boolean result = port.updateFaculty(fdata);

System.out.println("Result = "+result);

} catch (Exception ex) {

// TODO handle custom exceptions here }

………

A B C D

Figure 9.58. The complete codes for the cmdUpdateActionPerformed() method.

private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:

ArrayList al = new ArrayList();

al.clear();

al.add(0, NameField.getText().toString());

al.add(1, OfficeField.getText().toString());

al.add(2, PhoneField.getText().toString());

al.add(3, CollegeField.getText().toString());

al.add(4, TitleField.getText().toString());

al.add(5, EmailField.getText().toString());

al.add(6, ComboName.getSelectedItem().toString());

try { // Call Web Service Operation

org.ws.sql.WebServiceSQLService service = new org.ws.sql.WebServiceSQLService();

org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();

// TODO initialize WS operation arguments here Boolean update = port.updateFaculty(al);

if (!update) {

msgDlg.setMessage("The data updating is failed!");

msgDlg.setVisible(true);

} else

ComboName.addItem(NameField.getText());

}

catch (Exception ex){

System.out.println("exception: " + ex);

} } A B

C

D

c09.indd 829

c09.indd 829 7/20/2011 11:12:51 AM7/20/2011 11:12:51 AM

www.traintelco.com

Figure 9.59. The automatically created codes by NetBeans IDE.

………

try { // Call Web Service Operation

org.ws.sql.WebServiceSQLService service = new org.ws.sql.WebServiceSQLService();

org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();

// TODO initialize WS operation arguments here java.lang.String fname = "";

// TODO process result here

java.lang.Boolean result = port.deleteFaculty(fname);

System.out.println("Result = "+result);

} catch (Exception ex) {

// TODO handle custom exceptions here }

………

A B C D

this method. The running result of that operation is returned and assigned to a Boolean variable update .

B. If the value of the variable update is false , which means that no row has been updated in our Faculty table and this data updating has been failed, the msgDlg instance is used to show this situation.

C. Otherwise, if the value of the update variable is true , which means that this data updating action is successful, the updated faculty name will be added into the Faculty Name combo box ComboName using the addItem() method.

D. The catch block is used to track and display any possible exception during this Web service operation execution.

Next, let ’ s build the codes to perform the faculty data deleting action.

9.12.2.2 Build the Codes to Call the DeleteFaculty() Operation

Open our Windows - based client project WinClientSQL and double click on our main class FacultyFrame.java to open it. Click on the Design button to open the graphic user interface. In this client project, we want to use the Delete button in this form as a trigger to start the faculty data deleting action. Therefore, double click on the Delete button to open its event method cmdDeleteActionPerformed() .

Now let ’ s insert the codes that are related to calling the DeleteFaculty() operation in our Web service and created automatically by the NetBeans IDE by dragging this operation node into this method. Perform the following operations to complete this code creation process:

1. Expand the Web Service References node in our client project WinClientSQL and all subnodes under this node until the WebServiceSQLPort .

2. Drag the DeleteFaculty operation under this node and place it into our opened method

cmdDeleteActionPerformed() . A piece of codes shown in Figure 9.59 has been automati- cally created and added into this method.

It is unnecessary to explain the function of this piece of codes line by line since all of coding lines have been illustrated by the built - in comments.

Now let ’ s do some modifi cations to this piece of codes and add some codes to meet our data deleting requirements. Enter the codes that are shown in Figure 9.60 into this method. The modifi ed codes have been highlighted in bold.

Let ’ s have a closer look at this piece of codes to see how it works.

A. The Web service operation DeleteFaculty() is executed with the selected faculty name as the argument of this method. The running result of that operation is returned and assigned to a Boolean variable delete .

B. If the value of the variable delete is false , which means that no row has been deleted from our Faculty table and this data deleting has failed, the msgDlg instance is used to show this situation.

C. The catch block is used to track and display any possible exception during this Web service operation execution.

At this point, we have completed all coding development for our Windows - based client project for the data updating and deleting actions. Now let ’ s build and run our client project to call and test our Web service to perform faculty data updating and deleting actions.

Một phần của tài liệu practical database aprogramming with java (Trang 700 - 704)

Tải bản đầy đủ (PDF)

(791 trang)