7.2.2 Develop a Method for Data Checking Prior to Data Insertion
Create a new method named chkFaculty() and enter the code shown in Figure 7.3 in this method. Let’s take a closer look at this piece of code to see how it works.
A. A Java TextField array, f _ field[], is declared and initialized with seven TextFields that will be filled by seven pieces of faculty information later. The purpose of this setting is to simplify the data checking process later.
B. A for loop is used to check all TextFields, that is, to use a system method, getText(), to do this check to make sure that all of them are filled, without any empty ones. A false is returned if any field is empty to indicate this error.
Next, let’s take care of how to insert a selected faculty image into the database.
7.2.3 Develop a Method for Selecting a Valid Faculty Image
When performing this data insertion, in addition to the seven pieces of a new faculty information, a new or a default faculty image should also be included. It is crucial to get this image in a simple way to speed up this insertion action. To that purpose, create another user-defined method, getFacul-tyImage(), and enter the code shown in Figure 7.4 for this method.
FIGURE 7.3 The detailed code for the user-defined method chkFaculty().
FIGURE 7.4 The detailed code for the user-defined method getFacultyImage().
Let’s take a closer look at this piece of code to see how it works.
A. Some local variables are declared first, which include a byte array, fimage, and a File object, imgFile. The former is used to hold the selected faculty image, and the latter is used to keep the selected faculty image in a file format.
B. A JFileChooser object, imgChooser, is created, and it is used to assist users to select a desired faculty image via a File Dialog.
C. To get and save a selected faculty image or a file, the current folder with the path is neces-sary, and this folder is our current project folder. The selected faculty image is stored in that folder to enable the system to pick it up and display it later.
D. A JFileChooser dialog is opened to allow users to select the desired faculty image.
E. If this dialog is opened successfully and a faculty image is selected, the property APPROVE _ OPTION is returned with a result whose value is non-zero. Then a system method, getSelectedFile(), is executed to return the selected image and assign it to the File object, imgFile, which is to be used in next step.
F. For debugging purposes, the file name and its path are displayed here.
G. A try-catch block is used to convert the image from the File format to the byte array format, since this is the allowed format for the fimage column in the database. To do that, a system method, readAllBytes(), is used. The argument of this method is the path of the selected image file.
H. A catch block is used to catch and report any possible exceptions if they occur.
I. Finally, the converted faculty image in byte array format is returned.
Before we can handle the last user-defined method, clearFaculty(), let’s take a look at an issue related to the Insert button. As we know, this button is disabled after a data insertion is done to avoid any possible duplicated insertion in step K in Figure 7.2. One question is: When should this button be enabled to allow users to begin a new insertion?