Develop the Code to Call Our Web Service Project 2 – Develop Java Web Services to Access Databases

The code related to steps 1~5 is shown in steps 1~4 in Figure 9.50. Add the Java code shown in steps A~E in Figure 9.50 into this block.

Let’s have a closer look at this code to see how it works.

A. An additional system method, getParameter(), is used to get the name of the selected faculty image, which is located at the Faculty _ Image tag in the view class Faculty. jsp. The name of the selected faculty image will be automatically assigned and stored to the Faculty _ Image tag in the view class as soon as the image is selected.

B. A new String array, fnew, is created, and it is used to hold eight pieces of new faculty information stored in the eight local String variables.
C. The InsertFaculty() method defined in our Java Bean is executed to insert these eight pieces of faculty information as a new faculty record into the Faculty Table. The eight pieces of new faculty information are stored in the String array fnew that works as the argument for this method. The run result of this method is returned and assigned to the local integer variable res.

D. If the run result is 0, it means that no record has been inserted into the Faculty Table, and this data insertion fails. In that case, we need to re-display the Faculty.jsp page to enable users to re-insert the faculty record.

E. If the run result is non-zero, it means that the new faculty record has been inserted into the Faculty Table. We need to clean up all seven fields that contain seven pieces of new inserted faculty information in the Faculty.jsp page to enable users to either test this insertion or insert another faculty record. A for() loop is used for that purpose.

Now let’s do our code for the Java managed bean class FacultyMBean.java.
Open our Web-based client project, WebClientFaculty _ Insert, and double-click on the FacultyMBean.java in the Projects window to open the managed bean class file. Let’s develop the code for the Insert() method in this class to fulfill this data insertion function.

Browse to the Insert() method and enter the code, as shown in Figure 9.51, into this method.

Let’s have a closer look at this code to see how it works.

A. Some local variables, such as an integer variable numInsert and an ArrayList instance al, are created. The first variable is used to hold the run result of this data insertion, and the second variable is used to pick up and reserve the input new faculty data array.

B. The clear() method is executed to make sure that the ArrayList instance is clean before a new faculty record is collected.

C. The add() method is used to pick up and add eight pieces of new faculty information into this new ArrayList instance al. Eight pieces of new faculty information are entered and selected by the user in the JSP page Faculty.jsp and stored in eight tags defined in the view class Faculty.jsp page.

D. A try-catch block is used to call the data insertion operation InsertFaculty() defined in our Web Service project. First a service instance with its service port is generated.

E. The InsertFaculty() operation in our Web service is called with the ArrayList instance that contains eight pieces of new faculty information as the argument. The exe-cution result of this faculty data insertion is returned and assigned to the local Boolean variable insert.

F. If the returned Boolean variable insert is false, which means that this data insertion fails, a system method, System.out.println(), is used to indicate this.

G. The catch block is used to catch any possible exception during the data insertion process.
H. Finally the run result of this data insertion is returned to the calling method.

Now let’s build and run our Web client project to call our Web service operation to perform the faculty data insert action.

FIGURE 9.51   The modified code for the Insert() method.

Build and Run Our Client Project to Insert Faculty Data via Web Service – Develop Java Web Services to Access Databases

9.10.3   Build and Run Our Client Project to Insert Faculty Data via Web Service

Prior to building and running this Web application, make sure that the new faculty image to be inserted into our database has been stored in your local folder, such as C:\Faculty Images. All faculty images can be found in the folder Images\Faculty in the Students folder on the CRC Press ftp site. One can copy and paste them to your local folder. Also make sure that our Web Service application project, WebAppFaculty, has been built and deployed.

Now click on the Clean and Build Main Project button to build our client project. If everything is fine, click on the Run Project button (green arrow) to run our client project.

On the opened view class Faculty.jsp page, enter the following data as a new faculty record into the seven related TextFields and select the desired faculty image.

Faculty ID:D58996
Name:Charley David
Title:Professor
Office:MTC-114
Phone:750–378–1500
College:University of Miami
Email:[email protected]
FimageDavid.jpg

Then click on the Browse button in the File Selection box (right on the Image label) to select a desired faculty image, David.jpg, from your local image folder. The finished new faculty record is shown in Figure 9.52.

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.

FIGURE 9.52   Seven pieces of new inserted faculty information.

FIGURE 9.53   The confirmation of a new faculty record insertion.

To confirm this data insert, two methods can be used. The first way is to perform another query for the selected faculty Charley David by typing this name into the Faculty Name box and clicking on the Select button. Now you can find that seven pieces of new inserted faculty infor-mation have been retrieved and displayed in this page with the selected faculty image, as shown in

Figure 9.53. Click on the Back button to terminate our project.

FIGURE 9.54   The new inserted faculty record.

The second way to confirm this data insertion is to open our Faculty Table using the Services window in the NetBeans IDE to check whether this new faculty record has been inserted. To do that using the Services window in the NetBeans IDE 12.0:

1) Open the Services window and expand the Databases node.
2) Right-click on our sample Oracle database URL: jdbc:oracle:thin:@local host:1521:XE;, and select the Connect item to connect to our database. You may need to enter a valid password, oracle _ 18c, to complete the database connection action.

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.54.
It can be seen that the new faculty record, Charley David, with the faculty _ id of D58996, which is located at the ninth row, has been successfully inserted into our database.

Our Web client project to consume our Web service WebServiceFaculty is successful! A complete Web client project, WebClientFaculty _ Insert, can be found in the folder Class DB Projects\Chapter 9 that is located in the Students folder at the CRC Press ftp site (refer to Figure 1.2 in Chapter 1).

Next let’s discuss how to build a Web service to perform data updating and deletion against our sample Oracle database.

BUILD JAVA WEB SERVICE TO UPDATE AND DELETE DATA FROM THE ORACLE DATABASE 2 – Develop Java Web Services to Access Databases

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

A. A local integer variable, numUpdated, is created first, and this variable is used to hold the run result of execution of the data update operation.

B. An instance of the FileInputStream class, fis, is generated and it is used to convert the updated faculty image to a FileInputStream format, and the converted image file can be updated to the database later.

C. A new File instance, fimage, is also declared and initialized with the path or location of the updated faculty image, which is located at the sixth position with an index of 6 on the input ArrayList that contains all eight pieces of updated faculty information. A point to be noted is that the variable in the seventh position (7) in the input ArrayList is the faculty _ id parameter.
D. The update query string is created with eight positional parameters. The query criterion is the faculty _ id that is the eighth positional parameter and placed after the WHERE clause.

E. A try-catch block is used to perform this data update action. First a user-defined method, DBConnection(), is called to set up a connection between our Web service and our sample database. A connection instance, con, is returned after the execution of this method.

F. A new PreparedStatement instance, pstmt, is created to perform this update query.

G. Seven setString() methods are used to set up the actual values for the seven positional dynamic updated parameters in the update query statement. One point to be noted is that the order of these setString() methods is not continuous from 1 to 8 because the sev-enth positional parameter is the updated faculty image, which will be processed separately, and the eighth positional parameter is the faculty _ id.

H. Another try-catch block is used to convert the updated faculty image to the FileInputStream format and make it ready to be written into the database.

I. The catch block is used to check any possible exception for this conversion. The excep-tion information will be recorded into a system log file if it occurs.
J. The converted faculty image is written into the seventh positional dynamic parameter via a setBinaryStream() system method.
K. The update action is performed by calling the executeUpdate() method, and the update result is returned and stored in the local integer variable numUpdated.
L. The database connection is closed by executing the close() method since we have com-pleted our data update action and need to disconnect our database.

M. The executeUpdate() method will return an integer to indicate whether this data update is successful or not. If a non-zero value is returned, which means that at least one row has been updated in our Faculty Table and this data update action is successful, a true is returned to the client project.

N. Otherwise, if a zero is returned, it means that no row has been updated in our sample data-base and this data update fails. A false is returned for this situation.
O. The catch block is used to track and display any exception during this data update pro-cess, and a false will be returned if one occurs.

Before we can build a Window-Based or Web-Based project to consume this Web Service proj-ect to test the data update function, let’s take care of the code for the data delete action against our sample database using the Web service operation DeleteFaculty().

BUILD JAVA WEB SERVICE TO UPDATE AND DELETE DATA FROM THE ORACLE DATABASE – Develop Java Web Services to Access Databases

9.11  BUILD JAVA WEB SERVICE TO UPDATE AND DELETE DATA FROM THE ORACLE DATABASE

It is straightforward to perform data update and delete actions against our sample Oracle data-base via Web service, and we can add two more new operations, UpdateFaculty() and DeleteFaculty(), into our Web service project WebServiceFaculty we built in the previous sections. First let’s concentrate on the faculty data update action.

One key point to be noted when performing a data update action is that in most real applications, a completed faculty record should be updated, except the faculty _ id, since it is much easier to insert a new faculty record with a new faculty _ id than to update a record with an updated faculty _ id because of the complexity in cascaded update relationships we built in Chapter 2 when we created our sample database. Therefore, in this section, we will concentrate on the updat-ing a faculty record based on an existing faculty _ id.

9.11.1  Add a New Operation UpdateFaculty() to Perform Faculty Data Update

Perform the following operations to add a new operation, UpdateFaculty(), into our Web ser-vice project, WebServiceFaculty:
1) Launch NetBeans IDE 12.0 and open our Web application project, WebAppFaculty, and our Web service main class file, WebServiceFaculty.java, in the Projects window.

2) Click on the Design button at the top of the window to open the Design View of our Web service class file, WebServiceFaculty.java.
3) Click on the Add Operation button to open the Add Operation wizard.
4) Enter UpdateFaculty into the Name field and click on the Browse button next to the Return Type combo box. Type boolean into the Type Name field and select the item Boolean (java.lang) from the list, and click on the OK button.

5) Click on the Add button and enter fdata into the Name parameter field. Then click on the dropdown arrow of the Type combo box, and select the Choose item to open the Find Type wizard. Type Arraylist into the top field and select the ArrayList (java. util) data type, and click on the OK button to select an ArrayList as the data type for the input parameter.

Your finished Add Operation wizard should match the one shown in Figure 9.55. Click on the OK button to complete the new operation creation process.

Click on the Source button on the top of this window to open the code window of our Web service project. Let’s develop the code for this new added operation.
In the opened code window, enter the code shown in Figure 9.56 into the new operation.

FIGURE 9.55   The complete Add Operation wizard.

FIGURE 9.56   The code for the new operation, UpdateFaculty().