Update and Delete Data from the Faculty Table Using JSP and Java Beans Techniques – Develop Java Web Applications to Access Databases

8.5.3  Update and Delete Data from the Faculty Table Using JSP and Java Beans Techniques

To use the JavaServer Pages and Java bean techniques to perform data updating and delete actions against the Faculty Table, we need to perform the following operations:

1) Create a new Java Session bean class, FacultyUpdateDeleteBean.java, to handle the data updating and delete actions.

2) Modify the model controller page, FacultyProcess.jsp, to handle the faculty data collection and manipulation.

Now let’s create a new project, JavaWebOracleUpdateDelete, based on our existing proj-ect, JavaWebOracleInsert. Perform the following operations to complete this creation:
1) In the Projects window, right-click on the project JavaWebOracleInsert and select the Copy item from the popup menu to open the Copy Project wizard, as shown in Figure 8.88.
2) Enter our new project name, JavaWebOracleUpdateDelete, into the Project Name box; browse to the default project folder, C:\Class DB Projects\Chapter 8, as the Project Location, which is shown in Figure 8.88; and click on the Copy button.

A new project, JavaWebOracleUpdateDelete, is generated and added into our Projects window. Next let’s build the code for the related pages to update or delete records in the Faculty Table in our sample database.

First let’s create our Java session bean class FacultyUpdateDeleteBean.java to handle the data updating and delete actions.

FIGURE 8.88   The copied project JavaWebOracleUpdateDelete.

8.5.3.1  Create a New Java Session Bean Class

Perform the following operations to create a new Java session bean class:

1) Right-click on our new project, JavaWebOracleUpdateDelete, in the Projects
window and select the New > Java Class item from the popup menu to open the New
Java Class wizard.
2) Enter FacultyUpdateDeleteBean into the Class Name field and select
JavaWebOracleSelect Package from the Package combo box.
3) Keep all other default settings and click on the Finish button.

In the created FacultyUpdateDeleteBean.java class, we need to create two new methods, UpdateFaculty() and DeleteFaculty(). These two methods are used to perform the data updating and deleting operations against our sample database. Figure 8.89 shows the first part of the code used to perform the data update actions.

Let’s have a closer look at the code for these two methods to see how it works.

A. Some useful packages are imported first, since all Oracle database-related classes and
other components are defined in those packages.
B. Seven class properties related to the associated columns in the Faculty Table are declared first. These properties are very important, since they are directly mapped to the associ-ated columns in the Faculty Table. All of these properties can be accessed by using the associated getter() method defined in the second code part of this class. A class-level database connection object, con, is also created.

C. In the constructor of this class, a try-catch block is used to load the Oracle JDBC driver. The catch block is used to track and collect any possible exception during this database driver loading process.
D. The database connection URL is defined. Refer to Section 6.3.3.2 in Chapter 6 to get more
detailed information about this URL definition.
E. Another try-catch block is used to connect to our sample Oracle database with the desired username and password. The catch block is used to track and collect any possible exception during the database connection process.

F. The main data updating method, UpdateFaculty(), is defined with the selected faculty update information as the argument. This argument is a String array that contains all seven pieces of updated faculty information without faculty _ id. A local integer variable, numUpdated, and the update statement are first created, with the faculty ID as the posi-tional dynamic parameter.

Related Posts

Leave a Reply

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