JAVA RUNTIME OBJECTS METHOD
9.15 BUILD A WINDOWS - BASED WEB CLIENT PROJECT TO
9.15.4 Develop the Codes to Call Our Web Service Project
Figure 9.109. The fi nished New Web Service Client wizard.
between each button ’ s method in our client project and each operation defi ned in our Web service.
Let ’ s start our coding process from the fi rst button Select in our client project and its method cmdSelectActionPerformed() .
9.15.4.1 Build Codes for the Select Button Method to Query CourseIDs
The function of this method is to query all course_id taught by the selected faculty member as the Select button is clicked by the user. The queried result will be added and displayed in the Course ID List box in this CourseFrame form.
On the opened Design view of the CourseFrame form window, double click on the
Select button to open this method. Perform the following operations to add the Web service operation QueryCourseID() into this method:
1. Browse to the Web Service References node under our client project WinClientOracle . 2. Expand our Web service and its port until all our fi ve Web service operations have been
exposed.
3. Drag the QueryCourseID operation and place it inside the cmdSelectActionPerformed() method.
4. A piece of codes is automatically created and added into this method, as shown in Figure 9.110 .
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 course_id query requirements. Enter the codes that are shown in Figure 9.111 into this method, and the modifi ed codes have been highlighted in bold.
Let ’ s have a closer look at this piece of newly added codes to see how it works.
A. Two local variables, al and cResult , are created fi rst. The fi rst is an ArrayList instance that is used to hold the query result, and the second is a String array used to convert the query result and display it in the Course ID List .
B. The Web service operation QueryCourseID() is called to perform this course data query to collect all course_id taught by the selected faculty member that is obtained from the Table 9.11. The relationship between each button ’ s method and each operation
Select
cmdSelectActionPerformed() QueryCourseID() Query all course_id taught by the selected faculty CourseListValueChanged() QueryCourse() Query detailed information for selected course_id
Insert
cmdInsertActionPerformed() InsertCourse() Insert a new course record into the Course table Update
cmdUpdateActionPerformed() UpdateCourse() Update an existing course record in the Course table Delete
cmdDeleteActionPerformed() DeleteCourse() Delete a course record from the Course table
Client Button & Method Web Service Operation Function
c09.indd 877
c09.indd 877 7/20/2011 11:12:54 AM7/20/2011 11:12:54 AM
www.traintelco.com
ComboName combo box. The query result is returned and assigned to the local ArrayList instance al .
C. A for loop is used to pick up each course_id and assign it to each element in the cResult[]
array.
D. The converted query result is sent to the Course ID List variable, CourseList , to have it displayed in there using the setListData() method.
E. The catch block is used to track and display any possible exception during this course_id query process.
Now we have fi nished the coding process for calling the Web service operation QueryCourseID() to query all course_id based on the selected faculty member. Click on the Clean and Build Main Project button to build our project.
Click on the Run Main Project button to run our client project to test this course_id query function. Select the CourseFrame as our main class and click on the OK button to the Run Project dialog to run our project.
Figure 9.110. The automatically created and added codes.
A B C D E
private void cmdSelectActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracleService service = new org.ws.oracle.WebServiceOracleService();
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
// TODO initialize WS operation arguments here java.lang.String fname = "";
// TODO process result here
java.util.List<java.lang.Object> result = port.queryCourseID(fname);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here }
}
Figure 9.111. The modifi ed codes for the cmdSelectActionPerformed() method.
A
B C D E
private void cmdSelectActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
ArrayList<String> al = new ArrayList<String>();
String[] cResult = new String[6];
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracleService service = new org.ws.oracle.WebServiceOracleService();
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
// TODO process result here
al = (ArrayList)port.queryCourseID(ComboName.getSelectedItem().toString());
for (int col = 0; col < al.size(); col++) cResult[col] = al.get(col).toString();
CourseList.setListData(cResult);
} catch (Exception ex) {
msgDlg.setMessage("exception is: " + ex);
msgDlg.setVisible(true);
}
}
On the opened client project, keep the default faculty member Ying Bai unchanged and click on the Select button to query all course_id taught by this selected faculty.
Immediately, you can fi nd that all four courses or four course_id taught by this faculty have been returned and displayed in the Course ID List box, as shown in Figure 9.112 . You can try to query course_id for other faculty members. Our client project in querying course_id is successful.
Next, let ’ s take care of the coding for the CourseListValueChanged() method to query the detailed course information for a selected course_id from the Course ID List.
9.15.4.2 Build Codes for the CourseListValueChanged() Method to Get Course Details
The function of this method is that when a user clicks a course_id from the Course ID List box, the detailed course information, such as the course title, credit, classroom, sched- ule, and enrollment for the selected course_id , will be retrieved and displayed in six text fi elds in this CourseFrame form window.
Perform the following operations to build the codes for this method to perform this function:
1. Open our client project WinClientOracle if it has not been opened, and open our main GUI CourseFrame.java by double clicking on it.
2. Click on the Design button on the top of the window to open the GUI window, and right click on our Course ID List Listbox and select Events > ListSelection >
valueChanged item to open this method.
3. Go to the Projects window and browse to our Web Service References node; expand this node until all of our fi ve Web service operations are exposed. Drag the QueryCourse operation and place it into this method.
4. A piece of codes is created and added into this method, as shown in Figure 9.113 .
Figure 9.112. The running result of the course_id query.
c09.indd 879
c09.indd 879 7/20/2011 11:12:54 AM7/20/2011 11:12:54 AM
www.traintelco.com
It is unnecessary to explain the function of this piece of codes line by line since all 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 course query requirements. Enter the codes that are shown in Figure 9.114 into this method, and the modifi ed codes have been highlighted in bold.
Let ’ s have a closer look at this piece of new added codes to see how it works.
A. An ArrayList instance al is created fi rst, and it is used to collect the query result stored in an ArrayList object that is returned from the execution of the Web service operation
QueryCourse() .
B. A JTextField array cField[] is created and initialized with six text fi elds in this CourseFrame form. The purpose of this array is to store queried course details and display them in these six text fi elds.
C. Since the JList component belongs to the javax.swing package, not java.awt package, therefore, a clicking on an entry in the CourseList box causes the itemStateChanged () method to fi re twice. Once when the mouse button is depressed, and once again when it
Figure 9.113. The automatically created and added codes.
A B C D E
private void CourseListValueChanged(javax.swing.event.ListSelectionEvent evt) { // TODO add your handling code here:
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracleService service = new org.ws.oracle.WebServiceOracleService();
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
// TODO initialize WS operation arguments here java.lang.String courseID = "";
// TODO process result here
java.util.List<java.lang.Object> result = port.queryCourse(courseID);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here }
}
Figure 9.114. The modifi ed codes for the CourseListValueChanged() method.
A B C D
E F G
private void CourseListValueChanged(javax.swing.event.ListSelectionEvent evt) { // TODO add your handling code here:
ArrayList<String> al = new ArrayList<String>();
JTextField[] cField = {CourseIDField, CourseField, CreditField, ClassroomField, ScheduleField, EnrollField};
if(!CourseList.getValueIsAdjusting() ) {
String courseid = (String)CourseList.getSelectedValue();
if (courseid != null){
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracleService service = new org.ws.oracle.WebServiceOracleService();
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
// TODO initialize WS operation arguments here al = (ArrayList)port.queryCourse(courseid);
for (int col = 0; col < al.size(); col++) cField[col].setText(al.get(col).toString());
} catch (Exception ex) {
msgDlg.setMessage("exception is: " + ex);
msgDlg.setVisible(true);
} } }
}
is released. Therefore, the selected course_id will be appeared twice when it is selected.
To prevent this from occurring, the getValueIsAdjusting() method is used to make sure that no item has been adjusted to be displayed twice. Then the selected course_id is assigned to a local String variable courseid by calling the getSelectedValue() method of the CourseList Box class.
D. Before we can proceed to the course query operation, fi rst we need to confi rm that the selected courseid is not a null value. A null value would be returned if the user did not select any course_id from the CourseList box; instead, the user just clicked on the Select button to try to fi nd all courses taught by other faculty members. Even the user only clicked on the Select button without touching any course_id in the CourseList box; however, the system still considers that a null course_id has been selected and thus a null value will be returned. To avoid that situation from occurring, an if selection structure is used to make sure that no null value has been returned from the CourseList box.
E. The Web service operation QueryCourse() is called to perform this course data query to collect detailed course information for the selected course_id . The query result is returned and assigned to the local ArrayList instance al . A cast (ArrayList) is necessary for this assignment since the data type of al is an ArrayList < String > in this method.
F. A for loop is used to pick up each piece of detailed course information and assign it to each text fi eld in the cField[] array using the setText() method.
G. The catch block is used to track and display any possible exception during this course_id query 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 the coding process for calling the Web service operation QueryCourse() to query detailed course information based on the selected course_id . Click on the Clean and Build Main Project button to build our project.
Click on the Run Main Project button to run our client project to test this course query function.
On the opened client project, keep the default faculty member Ying Bai unchanged and click on the Select button to query all course_id taught by this selected faculty.
Immediately, you can fi nd that all four courses or four course_id taught by this faculty have been returned and displayed in the Course ID List box. To get course details for a selected course_id , just click on that course_id from the Course ID List. Figure 9.115 shows an example of course details for a course_id that is CSE - 438.
You can try to click the different course_id to get related detailed course informa- tion. Our client project in querying detailed course information is successful.
A point to be noted is that before you can run any client project to consume a Web service, make sure that the Web service has been successfully deployed and the Glassfi sh v3 server is running. To confi rm this, just build and deploy the Web service one more time before you run the client project to consume it, especially if you just start or restart the NetBeans IDE since the server will be stopped when the IDE is exited.
c09.indd 881
c09.indd 881 7/20/2011 11:12:54 AM7/20/2011 11:12:54 AM
www.traintelco.com
Next, let ’ s take care of the coding for the cmdInsertActionPerformed() method to insert a new course record into the Course table in our sample database.
9.15.4.3 Build Codes for the Insert Button Method to Insert Courses
The function of this method is to insert a new course record as the Insert button is clicked by the user. The new course record will be inserted into the Course table in our sample Oracle database when this method is complete.
On the opened Design view of the CourseFrame form window, double click on the
Insert button to open this method. Perform the following operations to add the Web service operation InsertCourse() into this method:
1. Browse to the Web Service References node under our client project WinClientOracle . 2. Expand our Web service and its port until all our fi ve Web service operations have been
exposed.
3. Drag the InsertCourse operation and place it inside the cmdInsertActionPerformed() method.
4. A piece of codes is created and added into this method, as shown in Figure 9.116 .
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 new course record insertion requirements. Enter the codes that are shown in Figure 9.117 into this method, and the modifi ed codes have been highlighted in bold.
Let ’ s have a closer look at this piece of new added codes to see how it works.
A. Two local variables, insert and al , are created fi rst. The fi rst one is a Boolean variable used to hold the running result of the execution of the Web service operation InsertCourse() ,
Figure 9.115. Running result for an example course_id CSE - 438.
and the second is an ArrayList instance used to store a new course record to be inserted into the Course table in our sample database.
B. The ArrayList instance al is cleaned up using the clear() method to make sure that the al is empty before it can store any data.
C. A group of add() methods are used to add seven pieces of new course information into the ArrayList instance. One point to be noted is that the order in which to add these course parameters must be identical with the order of assigning these parameters to the Course object in the session bean method newCourse() in our Web service project
WebServiceOracle . Refer to that method to make sure that both orders are identical. An optional way to do this assignment is to create a JTextField array and use a for loop.
D. The Web operation InsertCourse() is called to insert this new course record stored in the argument al into the Course table via our Web service. The execution result that is a Boolean variable is returned and assigned to the local variable insert .
Figure 9.116. The automatically created and added codes.
A B C D E
private void cmdInsertActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracleService service = new org.ws.oracle.WebServiceOracleService();
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
// TODO initialize WS operation arguments here java.util.List<java.lang.Object> cdata = null;
// TODO process result here
java.lang.Boolean result = port.insertCourse(cdata);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here }
}
Figure 9.117. The modifi ed codes for the cmdInsertActionPerformed() method.
A B C
D E F
private void cmdInsertActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
Boolean insert = false;
ArrayList al = new ArrayList();
al.clear();
al.add(0, CourseIDField.getText());
al.add(1, CourseField.getText());
al.add(2, CreditField.getText());
al.add(3, ClassroomField.getText());
al.add(4, ScheduleField.getText());
al.add(5, EnrollField.getText());
al.add(6, ComboName.getSelectedItem().toString());
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracleService service = new org.ws.oracle.WebServiceOracleService();
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
insert = port.insertCourse(al);
if (!insert)
System.out.println("Error in course insertion...");
} catch (Exception ex) {
msgDlg.setMessage("exception is: " + ex);
msgDlg.setVisible(true);
} }
c09.indd 883
c09.indd 883 7/20/2011 11:12:54 AM7/20/2011 11:12:54 AM
www.traintelco.com
Figure 9.118. The new course record to be inserted.
E. If a false is returned, which means that this course data insertion has been failed, the system println() method is used to indicate this situation.
F. The catch block is used to track and display any possible exception during this data inser- tion process.
Now we have fi nished the coding process for calling the Web service operation InsertCourse() to insert a new course record into the Course table based on the selected faculty member. Click on the Clean and Build Main Project button to build our project.
Click on the Run Main Project button to run our client project to test this course data insertion function.
On the opened client project, keep the default faculty member Ying Bai unchanged and click on the Select button to query all course_id taught by this selected faculty.
Immediately, you can fi nd that all four courses or four course_id taught by this faculty have been returned and displayed in the Course ID List box. To insert a new course, enter six pieces of new course information shown below into six text fi elds.
Your fi nished CourseFrame window is shown in Figure 9.118 .
Click on the Insert button to insert this new course record into the Course table in our sample database.
To test this new course insertion, there are more than one way can be used. The fi rst way is to open the Course table to confi rm this new course insertion. But the second way,
• Course ID: CSE - 549 • Course: Fuzzy Systems • Schedule: T - H: 1:30 – 2:45 PM • Classroom: TC - 302
• Credit: 3 • Enrollment: 25