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

G. A FileInputStream object, fis, is created and works as a handler of the InputStream for the updated faculty image later.

H. A new File object, fimage, is also generated to convert the updated faculty image that is stored in a string variable, upFaculty[6], to a file object, and it will be further converted to an InputStream object later to be written into the Faculty Table.
I. The updated query string is generated with eight positional parameters to make this update
action ready.

J. Starting from a try block, the prepareStatement() method is called to create a PreparedStatement object, pstmt.
K. Six setter methods are used to set the positional parameters in the update statement with the positional order. This order must be identical to that defined in the input argument upFaculty[], which is a String array.
L. With another try-catch block, a new FileInputStream object, fis, is generated with the File fimage as the argument to convert that File to a FileInputStream object and assign it to that new created object. The catch block is used to check and inspect any possible exception for this file if it cannot be found.

FIGURE 8.89   The first part of the code of the Java bean class file.

FIGURE 8.90   The second part of the code of the Java bean class file.

M. Then a system method, setBinaryStream(), is executed to assign the faculty image file as a binary stream sequence to the seventh input position parameter, fimage, to the Update query statement.

N. The setString() method is executed again to setup the eighth input positional param-eter, faculty _ id, to the query WHERE clause in the Update query statement.

O. The executeUpdate() method is executed to perform this data update action, and the returned result, which is the number of the rows that have been successfully updated in the Faculty Table, is assigned to the local integer variable, numUpdated.

P. The catch block is used to track and collect any possible exceptions during this data update operation.
Q. The data update result is returned to the calling method.

The second part of the code for this Java bean class is shown in Figure 8.90. Let’s have a closer look at this piece of code to see how it works.

A. Starting from step A, including steps C through G, seven getter methods are defined, and they are used to pick up all seven properties defined at the beginning of this class.

In fact, the code for this Java bean class is similar to that we built in our Java help class file, which includes loading the JDBC driver, defining the database connection URL, connecting to database and executing the appropriate method to perform related data actions to our database.

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

Related Posts

Leave a Reply

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