Blog / Create and Build the Web Operation DeleteCourse() – Develop Java Web Services to Access Databases

Create and Build the Web Operation DeleteCourse() – Develop Java Web Services to Access Databases

Create and Build the Web Operation DeleteCourse() – Develop Java Web Services to Access Databases


9.14.4.6  Create and Build the Web Operation DeleteCourse()
Perform the following operations to add a new operation, DeleteCourse(), into our Web service project to perform the course delete query:

1) Double-click on our Web service main class file, WebServiceCourse.java, from the Projects window to open it.
2) Click on the Design button at the top of the window to open the Design View of our Web service class, WebServiceCourse.java.
3) Click on the Add Operation button to open the Add Operation wizard.
4) Enter DeleteCourse into the Name field and click on the Browse button that is 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 cid into the Name parameter field. Keep the default Type, java.lang.String, as the data type for this input parameter. Your finished Add Operation wizard should match the one shown in Figure 9.84.

6) Click on the OK button to create this new operation.

Next let’s build the code for this operation.

FIGURE 9.84  The completed Add Operation wizard.

FIGURE 9.85   Detailed code for the operation DeleteCourse().

Click on the Source button at the top of this window to open the code window of our Web ser-vice class file. In the opened code window, enter the code shown in Figure 9.85 into the new added operation, UpdateCourse().
Let’s have a closer look at this piece of new added code to see how it works.

A. A local integer variable, numDelete, is declared, and it is used to hold our query result.
B. A try-catch block is used to do our delete query for the Course Table to remove the target course record based on the selected course _ id. First a valid database connec-tion is established by calling our user-defined method, DBConnection().

C. A PreparedStatement object, pstmt, is generated with a connection object, con, and the delete statement with a selected course _ id as a positional dynamic input parameter.

D. A system method, setString(), is executed to set up the input positional dynamic parameter cid, which is the desired course _ id and works as a query criterion for this delete action.

E. The delete action is then executed by calling a system method, executeUpdate(), and the run result of this action is returned and assigned to our local variable, intDelete.

F. A cleaning job is performed to close the connection object we used for this query.
G. If this course delete is successful, a non-zero integer value would be returned, and a true is also returned to the consuming project to indicate success.

H. Otherwise, if a zero is returned, which means that the data delete fails and no record has been deleted from our database, a false is returned to indicate this error.
I. The catch block is used to monitor and track this query operation. A false would be returned if any exception occurred.

One can click on the Clean and Build Project button to build our Web Service project to update this new operation. Now we have completed all developments for our Web Service project. A complete Web Service application project, WebAppCourse, can be found in the folder Class BD Projects\Chapter 9 in the Students folder at the CRC Press ftp site.

Next we can build some Windows-based or Web-based projects to consume these operations to perform related database actions to query, insert, and update and delete records from the Course Table in our sample database. First let’s start with a Windows-based project.

Angela Gaffney

  • 0

Leave a Reply

Your email address will not be published. Required fields are marked *


                Copyright © 2024 Saucierlight.com. All Rights Reserved.