
FIGURE 9.43 The code for the new operation, InsertFaculty().
B. An instance of the FileInputStream class, fis, is generated, and it is used to convert the inserted faculty image to a FileInputStream format and inserted into the database
later.
C. A new File instance, fimage, is also declared and initialized with the path or loca-tion of the inserted faculty image, which is located at the eighth position with an index of 7 in the input ArrayList that contains all eight pieces of inserted faculty information.
D. The INSERT query string is generated with eight pieces of inserted faculty information represented by eight position parameters.
E. A try-catch block is used to start this insertion action. First a valid database connec-tion is established by calling a user-defined method, DBConnection().
F. A new instance of PreparedStatement class, pstmt, is declared with seven setString() methods to set up the actual values for seven positional dynamic parameters in the insert query statement. One point to be noted is that the order of these setString() methods must be identical to the order of columns in our Faculty Table.
G. Another try-catch block is used to convert the inserted faculty image to the FileInputStream format and make it ready to be written into the database.
H. The catch block is used to check any possible exception for this conversion.
I. The exception information will be recorded into a system log file if one occurs.
J. The converted faculty image is written into the eighth positional dynamic parameter via a setBinaryStream() system method.
K. The insert action is performed by calling the executeUpdate() method, and the insert result is returned and stored in the local integer variable numInsert.
L. Some used instances, including the PreparedStatement and Connection classes, are closed since we have completed our data insertion and need to disconnect our database.
M. The executeUpdate() method will return an integer to indicate whether this data insertion is successful. If a non-zero value is returned, which means that at least one row has been inserted into our Faculty Table and this data insert action is successful, a true is returned to the client project.
N. Otherwise, no row has been inserted into our sample database, and the data insertion fails. A false is returned for that situation.
O. The catch block is used to track and display any exception during this data insert opera-tion, and a false will be returned if one occurs.
You may experience some compiling errors during this coding process, which is normal, and all of these errors are due to missing imported packages. To fix them, just right-clickany place inside the code window and select the Fix Imports item from the popup menu.
At this point, we have completed all code development for the data insertion action. Now let’s build and run our Web service project to test its function.