7.3.2   Develop the Code for the Update Button Event Handler

We want to use the Update button built in the FacultyFrame form window to perform the faculty update function; therefore, no other modification to the FacultyFrame form window needs to be made. Now let’s develop the code for the Update button click event handler.

Open this event handler and enter the code shown in Figure 7.11 into this event handler. Let’s have a closer look at this piece of code to see how it works.

A. A local variable, numUpdated, and a byte array object, fImage, are created first. The variable numUpdated holds the run result of the data updating, and the byte array is used to hold a returned faculty image by calling the getFacultyImage() method later.

B. The user-defined method getFacultyImage() is executed to obtain an updated faculty image that can be selected by the user. The detailed code and an introduction to that user-defined method can be found in Section 7.2.3.

C. The update query string is created with eight positional parameters. The query criterion is the faculty ID, which is the eighth parameter and placed after the WHERE clause.
D. A try-catchblockisusedtoassistthisdataupdateaction.First,aPreparedStatement instance is created using the Connection object that is located in the LogInFrame class with the update query string as the argument.

E. A set of setString() methods is used to initialize the six pieces of updated faculty infor-mation, which are obtained from six TextFields and entered by the user as the project runs.
F. A setBytes() method is used to assign a byte array, fImage, to the fimage column in the Faculty Table as an updated faculty image. This method is very important, and only

FIGURE 7.11   The developed code for the Update button click event handler.

a byte array can be used to hold an image to be stored to the related image column in the database.

G. The eighth input parameter in the query string, faculty _ id, is assigned to the query criterion that is located after the WHERE clause.

H. The data update action is performed by calling the executeUpdate() method. The update result, which is an integer that is equal to the number of rows that have been updated by this data update action, is returned and assigned to the local integer variable numUpdated.
I. The catch block is used to track and collect any possible exception encountered when the data update is executed.
J. The run result is printed out for debugging purposes.
K. The user-defined method CurrentFaculty() is executed to retrieve all updated faculty names and add them into the Faculty Name combo box to enable users to validate this data update later.

Now let’s build and run the project to test the data update action.