Add a Method to the Java Bean to Perform Faculty Data Deleting – Develop Java Web Applications to Access Databases

8.5.3.3  Add a Method to the Java Bean to Perform Faculty Data Deleting

To perform a faculty record delete action, we need to perform the following operations:

1) Add a new method to the Java session bean FacultyUpdateDeleteBean to handle the faculty record delete action.

2) Modify the FacultyProcess.jsp page to do the faculty data collection and delete action.

FIGURE 8.94   The code for the DeleteFaculty() method.

Let’s first add a new method, DeleteFaculty(), into our Java session bean class file,
FacultyUpdateDeleteBean, to handle the faculty record delete action. Create a new method,DeleteFaculty(), and enter the code shown in Figure 8.94 into this Java Bean class file.

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

A. A local integer variable, numDeleted, is created, and it is used to hold the run result of executing the DeleteFaculty() method in the Java bean class FacultyUpdateDeleteBean.java with the bean id DeleteFaculty.

B. The delete statement is created with the faculty _ name as the positional dynamic parameter.

C. A try-catch block is used to perform the data delete action. The prepareState-ment() method is called to create a PreparedStatement object, pstmt.
D. The setter method is used to setup the positional dynamic parameter faculty _ name. E. The executeUpdate() method is executed to perform the data delete action, and the run result, which is the number of the rows that have been successfully deleted from the Faculty Table, is assigned to the local integer variable numDeleted.

F. The catch block is used to track and collect any exceptions during the data delete operation.

G. The data delete result is returned to the calling method.

Let’s modify the FacultyProcess.jsp page to do the faculty data collection and delete actions.

Related Posts

Leave a Reply

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