BUILD A WINDOWS - BASED WEB CLIENT PROJECT

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

JAVA RUNTIME OBJECTS METHOD

9.6 BUILD A WINDOWS - BASED WEB CLIENT PROJECT

To save time and space, we can use a Windows - based project SQLSelectObject we devel- oped in Section 6.4 in Chapter 6 to build this client project. The project can be found from the folder DBProjects\Chapter 6 that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1 ).

9.6.1 Copy the FacultyFrame and MsgDislog Components as GUI s

Perform the following operations to create a GUI for our Windows - based client project

WinClientSQL to consume our Web service:

1. Launch NetBeans IDE and choose the File > New Project item.

2. Select Java and Java Application from the Categories and the Projects lists, respectively. Click on the Next button.

c09.indd 795

c09.indd 795 7/20/2011 11:12:49 AM7/20/2011 11:12:49 AM

www.traintelco.com

3. Name the project as WinClientSQL and select a desired folder to save this project. Uncheck the Create Main Class checkbox. Your fi nished Name and Location wizard should match the one that is shown in Figure 9.28 .

4. Go to the Wiley ftp site (refer to Figure 1.2 in Chapter 1 ), load and open the project

SQLSelectObject from the folder DBProjects\Chapter 6 .

5. On the opened project, right click on the Faculty Frame fi le FacultyFrame.java under the project package node, and select the Refactor > Copy item to copy this form fi le.

6. On the opened Copy Class — FacultyFrame wizard, select our new project WinClientSQL from the Project combo box and remove the 1 after the FacultyFrame from the New Name fi eld.

7. Your fi nished Copy Class — FacultyFrame wizard should match the one that is shown in Figure 9.29 .

8. Click on the Refactor button to make a refactoring copy for this frame fi le.

9. Return to our new project WinClientSQL , and you can fi nd that a copied FacultyFrame.

java fi le has been pasted in the default package in our project.

Since we may need to use this form to test the faculty data query, insertion, updating, and deleting actions via our Web service project, now let ’ s perform some modifi cations to our copied FacultyFrame form window to make it as our desired GUI in this project.

Open our copied FacultyFrame form window and perform the following modifi ca- tions to make this form as our desired GUI:

1. Remove the Query Method combo box and its label.

2. Add two more Text Fields and the associated labels shown in Table 9.1 .

Figure 9.28. The fi nished Name and Location wizard.

Your modifi ed FacultyFrame form window should match the one that is shown in Figure 9.30 .

Perform a similar operation to copy the MsgDialog.java fi le and paste it into our new client project. Next, let ’ s develop the codes to call our Web service to perform this faculty data query. However, before we can begin the coding process, we must fi rst set up or create a Web service reference for our WinClientSQL project to enable our project to recognize that Web service and to call it when it is instructed to do that.

9.6.2 Create a Web Service Reference for Our Windows - Based Client Project

Perform the following operations to set up a Web service reference for our client project:

1. Right click on our client project WinClientSQL from the Projects window, and select the

New > Other item to open the New File wizard.

2. On the opened New File wizard, select Web Services from the Categories and Web Service Client from the File Types list, respectively. Click on the Next button to continue.

3. Click on the Browse button for the Project fi eld, and expand our Web application project

WebServiceSQLApp , and click on our Web service project WebServiceSQL to select it.

Figure 9.29. The fi nished Copy Class wizard.

Table 9.1. Newly added objects in the FacultyFrame form

Type Variable Name Text

Label Label1 FacultyID

Text Field IDField

Label Label2 Name

Text Field NameField

c09.indd 797

c09.indd 797 7/20/2011 11:12:49 AM7/20/2011 11:12:49 AM

www.traintelco.com

Then click on the OK button to select this Web service. Your fi nished Web Service Client wizard should match the one that is shown in Figure 9.31 .

4. Click on the Finish button to complete this Web service reference set up process.

Immediately, you can fi nd that a new node named Web Service References has been created and added into our client project. Expand this node and you can fi nd the

Figure 9.30. The modifi ed FacultyFrame form window.

Figure 9.31. The fi nished New Web Service Client wizard.

associated Web service port and our Web service operation QueryFaculty() under that node.

Now let ’ s develop the codes for this project to call the Web service to perform the data query from the Faculty table in our sample database.

9.6.3 Develop the Codes to Call Our Web Service Project

The coding process is divided into two parts: the modifi cation to the original codes and creation of new codes. First, let ’ do some modifi cations to the original codes in this FacultyFrame class. Perform the following code modifi cations to make this project as our Web consuming project:

1. Double click on our new copied FacultyFrame.java fi le from our project to open it.

2. Click on the Source button on the top to open the code window.

3. Go to the constructor of this class, and remove all four query methods from the ComboMethod object.

4. Open the cmdSelectActionPerformed() method and remove all codes inside this method except the fi rst coding line and the codes in the last if block.

5. Add two more items, IDField and NameField , into the beginning of the f_fi eld[] JTextField array located at the fi rst coding line. Also, change the order of the rest of items in this array to the order listed below:

Offi ceField, PhoneField, CollegeField, TitleField, EmailField

to make them identical with the order of the columns in our Faculty table.

Now, let ’ s develop some new codes to perform the faculty data query by calling our Web service project.

On the Design view of the FacultyFrame form window, double click on the Select button to open its event method cmdSelectActionPerformed() . Then enter the codes that are shown in Figure 9.32 into this method. The newly added and 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. Two more items, IDField and NameField , have been added into the beginning of the JTextField[] array. The order of the rest of items should also be modifi ed to make them identical with the order of the columns in our Faculty table.

B. A new ArrayList instance al is created to receive and hold the query result.

C. A try catch block is used to call our Web service to perform the faculty data query opera- tion. First, a new Web service instance service is created based on our Web service class

WebServiceSQLService .

D. The getWebServiceSQLPort() method is executed to get the current port used by our Web service. This port is returned and assigned to a new Port instance port .

E. Before we can call our Web service, make sure that our ArrayList object al is empty by executing the clear() method.

F. The queryFaculty() method defi ned in our Web service is called to perform this faculty data query. Two points to be noted are: (1) the argument of this method is a selected

c09.indd 799

c09.indd 799 7/20/2011 11:12:49 AM7/20/2011 11:12:49 AM

www.traintelco.com

faculty name obtained from the getSelectedItem() method from the Faculty Name combo box ComboName . Since this method returns an object, a toString() method must be attached to convert it to a string. (2) An ArrayList cast must be used to make sure that the returned query result is in this ArrayList type, since an ArrayList < String >

type is used in our Web service project. The query result is assigned to our ArrayList instance al .

G. A for loop is used to pick up each column from the query result using the get() method.

Two points to be noted are: (1) the argument of the get() method indicates the index of each column in the returned query result that is a single row, and that the data type of this method is an object. Therefore, a toString() method must be attached to convert it to a string. (2) To assign each column to each item in the f_fi eld array, the setText() method must be used.

H. The catch block is used to track and display any possible exception during this Web service calling process.

During the coding process, you may encounter some real - time compiling errors. Most of these errors are introduced by missing some packages that contain classes or compo- nents used in this fi le. To fi x these errors, just right click on this code window and select the Fix Imports item to load and import those missed packages to the top of this code window.

Now we have fi nished all coding process for this faculty data query action. Before we can build and run our project to test its function, we need to copy and save all images used in this project, including both faculty and students ’ image fi les, to our current project folder. Perform the following actions to fi nish this image fi les processing:

1. Open the Images folder that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1 ), copy all image fi les from that folder.

Figure 9.32. The modifi ed codes for the cmdSelectActionPerformed() method.

A B C D E F G

H

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

javax.swing.JTextField[] f_field = {IDField,NameField, OfficeField, PhoneField, CollegeField, TitleField, EmailField};

ArrayList al = new ArrayList();

try {

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

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

al.clear();

al = (ArrayList)port.queryFaculty(ComboName.getSelectedItem().toString());

for (int col = 0; col < al.size(); col++) f_field[col].setText(al.get(col).toString());

}

catch (Exception ex){

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

}

if (!ShowFaculty()){

msgDlg.setMessage("No matched faculty image found!");

msgDlg.setVisible(true);

}

}

2. In the NetBeans IDE, open our project WinClientSQL and click on the Files button to open the Files window. Then right click on our project WinClientSQL and select the Paste item to paste all image fi les into our current project node WinClientSQL .

Now we are ready to build and run our client project to test its function to call our Web service to perform the faculty data query.

9.6.4 Build and Run Our Client Project to Query Faculty Data via Web Service

Click on the Clean and Build Main Project button to build our client project. If every- thing is fi ne, click on the Run Main Project button to run our client project.

A message box may be pup up to ask the main starting class, which is shown in Figure 9.33 . Select our FacultyFrame class as the starting class, and click on the OK button to run the project.

The FacultyFrame form window is displayed, as shown in Figure 9.34 .

Select a desired faculty member, such as Ying Bai , from the Faculty Name combo box, and click on the Select button to query the detailed information for this faculty via our Web service WebServiceSQL . The queried result is displayed in this form, as shown in Figure 9.34 .

Our Web service and client projects are very successful!

Next, let ’ s build a Web - based client project to consume our Web service

WebServiceSQL to perform the faculty data query action.

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

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

(791 trang)