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().