9.15.5 Build Code for the Insert Button Event Handler to Insert a New Course
The function of this method is to insert a new course record into the Course Table in our sample database when the Insert button is clicked by the user. A new course record will be inserted into our sample database when this method is complete.
To make thing simple, let’s modify our project WinClientCourse _ Select to make it our new project, WinClientCourse _ Insert. Perform the following steps to build our new Windows-based project, WinClientCourse _ Insert:
1) Browse to our project WinClientCourse _ Select, which can be found in the folder Class DB Projects\Chapter 9 in the Students folder on the CRC Press ftp site (refer to Figure 1.2 in Chapter 1), and copy and then save it to any local folder in your machine.

FIGURE 9.91 The completed code for the InsertButtonActionPerformed() event handler.
2) Open NetBeans IDE 12.0 and right-click WinClientCourse _ Select in your local folder and select the Copy item from the popup menu.
3) In the opened Copy Project wizard, change the project name to WinClientCourse _ Insert in the Project Name box, select your desired location from the Project Location box and click on the Copy button.
The main source file, CourseFrame.java, under our Source Packages folder may gener-ate some errors when this new project is created. The reason for that is due to a Web Reference issue. To fix this error, build and deploy our Web Service application project, WebAppCourse, and then rebuild our new project, WinClientCourse _ Insert.
Now let’s develop the code for the Insert button’s event handler to perform data insertions to the Course Table in our sample database. Open the Insert button’s event handler by double-clicking the button from Design view, and enter the code shown in Figure 9.91 into the handler.
Let’s have a closer look at this piece of code to see how it works.
A. A local Java TextField[] array, cf, and a variable, al, are created first. The former is used to hold all TextFields, and the latter is an instance of ArrayList class used to hold a
new course record to be inserted into our database by calling our Web service operation InsertCourse() later.
B. The ArrayList instance al is cleaned up by using the clear() method to make sure that al is empty before it can be used to store any data.
C. A group of add() methods are used to add seven pieces of new course information into the ArrayList instance. One point is that the order in which to add these course parameters must be identical to the order of the columns in the Course Table in our sample database.
D. A try-catch block is used to perform the call to our Web service operation, InsertCourse(), to insert this new course record into our database. First a new Web service instance, service, is created based on our Web service class, WebServiceCourse _ Service. Then the getWebServiceCoursePort() method is executed to get the current port used by our Webservice. This port is returned and assigned to a new port instance, port.
E. The Web operation InsertCourse() is called to insert this new course record stored in the argument al into the Course Table via our Web service. The execution result is returned and assigned to the local Boolean variable insert.
F. If a false is returned, which means that this data insertion fails, the system println() method is used to indicate this situation.
G. Otherwise, our data insertion action is successful if a true is returned. Then a for() loop is used to clean up all six TextFields to indicate that the data insertion is completed.
H. The catch block is used to track any possible exception during the data insertion process.