7.6.2.2  Develop the Code for the Update Button Click Event Handler
Open our project, OracleCallableUpdate, and the CourseFrame Form window in Design View; double-click on the Update button to open its event handler; and enter the code shown in Figure 7.38 into this handler.
Let’s have a closer look at this piece of code to see how it works.

A. An if block is used to distinguish whether the Java Callable Method has been selected.

B. If it is, a new CallableStatement instance is declared.
C. A try-catch block is used to perform the data update action using the CallableStatement method. The CallableStatement query string is created. Refer to Section 6.3.7.5.1 in Chapter 6 to get more detailed information about the structure and protocol of a CallableStatement query string. This is a dynamic query string with seven pieces of positional update infor-mation related to a new course; therefore, seven question marks are used as the position holders for those parameters.

D. A new CallableStatement instance is created by calling the prepareCall() method that is defined in the Connection class.

E. The dynamic query string is initialized with seven positional parameters, and the values of those parameters are entered by the user into the associated course-related text fields. The point to be noted is for two parameters, the credits (Integer) and enrollment (integer), both are

FIGURE 7.38   The code for the Update button click event handler.

defined as numbers. Therefore the associated setXXX() methods need to be used to initialize these two parameters. Since the Integer class belongs to the java.lang package, here a full name is used for these classes. You can import the Java.lang package at the top of the code window under the Package clause to remove those package names if you like.
F. The CallableStatement instance is executed to call the stored procedure to update the
selected course record in the Course Table in our sample database.

G. The catch block is used to collect any possible exception for this data update process. H. Finally, the selected course _ id from the CourseList Listbox is assigned to the

Course ID field to indicate the updated course.

Now let’s build and run the project to test the data update function. Click on the Clean and Build
Main Project button to build the project, and click the Run Main Project button to run the project.
Enter a suiTable username and password, such as jhenry and test, to the LogIn frame form
and select the Course Information from the SelectFrame window to open the CourseFrame
form window. Make sure that the Java Callable Method has been selected from the Query
Method combo box. Then click on the Select button to query the default course information for
the selected faculty member, Jenney King.
Now select course CSE-668 from the CourseList listbox and enter the following data into the
seven text fields as an updated course record for the selected course, CSE-668:

Faculty Name:Jenney King
Course ID:CSE-668
Course Name:Intelligent Controls
Credits:3
Classroom:TC-309
Schedule:M-W-F: 11:00–11:50 AM
Enrollment:28

Then click on the Update button to update this course record in the Course Table in our sample database. To confirm and validate this data update, click on the Select button to try to retrieve all courses taught by the selected faculty member, Jenney King. Click on course _ id CSE-668

FIGURE 7.39   The run result of the data update action.

from the CourseList box; all details about this updated course are displayed in six TextFields on the right, as shown in Figure 7.39.

Another way to confirm this data update action is to open the Course Table using the Services window in the NetBeans IDE. To do that, open the Services window, expand the Databases node and connect to our sample database by right-clicking on that URL and selecting the Connect item. Then expand that connected URL and our CSE _ DEPT database node and Tables nodes. Right-click on the Course Table and select View Data to open the Table. Scroll down along this Table, and you can see that course CSE-668 has been updated and is displayed on the last line of this Course Table.

It is highly recommended to recover this updated course record to its original values, since we want to keep our database clean and neat. You can do this by performing another update action using the Update button event handler in this project. Refer to Section 7.6.1.1 to get more details about the course record to recover course CSE-668.

A complete project, OracleCallableUpdate, that contains the data update functions using the CallableStatement object can be found in the folder Class DB Projects\Chapter 7 located in the Students folder at the CRC Press ftp site (refer to Figure 1.2 in Chapter 1).

Next let’s handle the data deletion using the CallableStatement object method.