BUILD A WINDOWS - BASED WEB CLIENT PROJECT TO

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

JAVA RUNTIME OBJECTS METHOD

9.9 BUILD A WINDOWS - BASED WEB CLIENT PROJECT TO

We can still use the Windows - based client project WinClientSQL we built in Section 9.6 to consume the Web service to perform faculty data inserting action. One point to be noted is that although a Web reference to our Web service has been established in Section 9.6 , we still need to refresh this Web reference, since our Web service project has been modifi ed by adding one more operation InsertFaculty() in our Web service. Otherwise,

Figure 9.42. The deployment result of our Web service project.

c09.indd 811

c09.indd 811 7/20/2011 7:59:13 PM7/20/2011 7:59:13 PM

www.traintelco.com

we would still use the old Web service that does not include this InsertFaculty() operation.

9.9.1 Refresh the Web Service Reference for Our Windows - Based Client Project

In order to call this InsertFaculty() operation in our Web service project WebServiceSQL , we need to refresh the Web reference in our Windows - based client project to use the updated Web service project. Perform the following operations to refresh the Web service reference:

1. Open our Windows - based client project WinClientSQL and expand the Web Service References node.

2. Right click on our Web service WebServiceSQLService and choose the Delete item to remove this old Web reference.

3. Right click on our Windows - based client project WinClientSQL and select the New > Web Service Client item to open the New Web Service Client wizard.

4. On the opened wizard, click on the Browse button that is next to the Project fi eld and expand our Web application WebServiceSQLApp . Then choose our Web service

WebServiceSQL by clicking on it, and click on the OK button.

5. Click on the Finish button to complete this Web service reference refreshing process.

Now that we have refreshed or updated the Web service reference for our Windows - based client project WinClientSQL , next, let ’ s develop the codes in our client project to call that Web service operation InsertFaculty() to perform faculty data insertion.

9.9.2 Develop the Codes to Call Our Web Service Project

Open the 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 Insert button in this form as a trigger to start the faculty data insertion action. Therefore, double click on the Insert button to open its event method cmdInsertActionPerformed() and enter the codes that are shown in Figure 9.43 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 new faculty data array.

B. The add() method is used to pick up and add seven pieces of new faculty information into this new ArrayList instance al . Seven pieces of new faculty information are entered by the user and stored in seven text fi elds in this FacultyFrame window form. The toString() method is used to convert each piece of new faculty information obtained using the

getText() method that returns an object data type to a String. 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 text fi elds, which must be identical with order of columns in our Faculty table.

C. A try catch block is used to perform the calling of our Web service operation InsertFaculty() to perform this faculty data inserting action. First, a new Web service instance service is created based on our Web service class WebServiceSQLService . Then the getWebSer- viceSQLPort() 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 .

D. The Web service operation InsertFaculty() is executed with the ArrayList instance al that has been fi lled with seven pieces of new faculty information as the argument of this method.

The running result of that operation is returned and assigned to a Boolean variable insert . E. If the value of the variable insert is false , which means that no row has been inserted into our Faculty table, and this insertion has been failed, the msgDlg instance is used to show this situation.

F. Otherwise, if the value of the insert variable is true, which means that this data insertion is successful, the newly inserted faculty name will be added into the Faculty Name combo box ComboName using the addItem() method.

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

Now let ’ s build and run our client project to call and test our Web service to perform faculty data inserting action.

Figure 9.43. The codes for the Insert button event method.

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

ArrayList al = new ArrayList();

al.clear();

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

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

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

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

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

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

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

try {

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

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

Boolean insert = port.insertFaculty(al);

if (!insert) {

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

msgDlg.setVisible(true);

} else

ComboName.addItem(NameField.getText());

}

catch (Exception ex){

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

}

} A

B

C

D E

F

G

c09.indd 813

c09.indd 813 7/20/2011 11:12:50 AM7/20/2011 11:12:50 AM

www.traintelco.com

Figure 9.44. The seven pieces of newly inserted faculty information.

9.9.3 Build and Run Our Client Project to Insert 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.

The FacultyFrame form window is displayed. First, let ’ s perform a faculty query action. 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 seven text fi elds.

Now, enter a new faculty record with seven pieces of new faculty information shown below into seven text fi elds, which is shown in Figure 9.44 .

Click on the Insert button to try to call our Web service operation InsertFaculty() to insert this new faculty record into the Faculty table in our sample database.

To confi rm this data insertion, two methods can be used. First, we can open our Faculty table using either the Services window in the NetBeans IDE or the SQL Server 2008 Management Studio to check whether this new faculty record has been inserted. To do that using the Services window in the NetBeans IDE, perform the following operations:

• Faculty ID: T56789 • Name: Tom Jeff • Title: Professor • Offi ce: MTC - 150 • Phone: 750 - 378 - 1500 • College: University of Miami • Email: tjeff@college.edu

Figure 9.45. The opened Faculty table in the NetBeans IDE.

1. Open the Services window and expand the Databases node.

2. Right click on our SQL Server database URL: jdbc:sqlserver://localhost\

SQL2008EXPRESS: 5000; databaseName=CSE_DEPT [ybai on dbo] , and select the

Connect item to connect to our database.

3. Expand our sample database CSE_DEPT and Tables .

4. Right click on the Faculty table and select the View Data item.

Your opened Faculty table is shown in Figure 9.45 .

It can be found that the new faculty record with the faculty_id of T56789 , which is located at the last row and has been highlighted in dark color, has been successfully inserted into our database.

The second way to confi rm this data insertion, which is simpler, is to use the Select button in this form to perform a query to try to retrieve the inserted faculty record.

To do this checking in second way, go to the Faculty Name combo box, and you can fi nd that the new faculty name Tom Jeff has been added into this box. Click it to select it, and click on the Select button. Do not worry about the exception message for the faculty image, since we did not insert any image for this newly inserted faculty. Just click on the OK button for that message box, and you can fi nd that seven pieces of newly inserted faculty information have been retrieved and displayed in this form window. Our data insertion is successful!

It is highly recommended to remove this newly inserted faculty record from our database since we want to keep our database clean. You can delete this record by opening the SQL Server 2008 Management Studio to do it.

Next, let ’ s build a Web - based client project to consume our Web service to insert a new faculty record into the Faculty table in our sample database.

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

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

(791 trang)