JAVA RUNTIME OBJECTS METHOD
7.4 PERFORM DATA MANIPULATIONS TO ORACLE DATABASE USING JAVA RUNTIME OBJECT
7.4.1 Perform Data Insertion to Oracle Database Using Java
In Section 6.5.1 in Chapter 6 , we have created a FacultyFrame class and Faculty JFrame window FacultyFrame. Also, the following components have been added into that project:
• A JDBC driver for Oracle database has been loaded and registered.
• A valid database connection to that project has been established.
• A PreparedStatement instance has been created and implemented in the Select button click event handler to perform the data query.
In this section, we want to use the Insert button that has been added into the FacultyFrame window to perform this data insertion function. First, let ’ s do some modi- fi cations to this FacultyFrame form window to enable us to perform the data manipulations.
7.4.1.1 Modify the FacultyFrame Window Form
First, let ’ s modify the FacultyFrame form by adding three more Text Fields into this frame:
two of them are added into the Faculty Information panel to enable us to insert a faculty record, and one of them is added at the top of the faculty image box to allow us to insert a new faculty image (exactly the location of the faculty image).
Perform the following operations to open our pasted project OracleSelectObject : 1. Launch the NetBeans IDE 6.8 and go to File > Open Project menu item to open
the Open Project wizard.
2. Browse to the location where we copied and pasted our project OracleSelectObject , which is JavaDBProject\Chapter 7 . Make sure that the Open as Main Project checkbox has been checked, and select this project and click on the Open Project button to open it.
c07.indd 503
c07.indd 503 7/20/2011 11:12:08 AM7/20/2011 11:12:08 AM
www.traintelco.com
The point to be noted is that you now have two OracleSelectObject projects in the NetBeans IDE, but they are different projects with different functions. The fi rst
OracleSelectObject was built in Chapter 6 without data manipulation function, but this second project will be built in Chapter 7 with the data manipulation function.
3. Expand this project fi les to open the FacultyFrame.java fi le by double clicking on this fi le that is located under the Source Packages\OracleSelectObject node.
4. Click on the Design button at the top of this window to open the GUI window of this FacultyFrame class.
Perform the following operations to add three more Text Fields into this frame window:
• Enlarge the FacultyFrame window form and the Faculty Information panel.
• Add two more labels and two more Text fi elds into this Faculty Information panel and one more label and the associated Text Field to the top of the Faculty Image box with the properties shown in Table 7.12 .
One point to be noted is the FacultyIDField , and its editable property is checked, which means that we need to modify the faculty_id as the project runs since we may insert a new faculty record, including a new faculty_id , as the project runs. However, this fi eld should be disabled when a data updating is performed, because we will not update it during a faculty record updating process. Your fi nished modifi ed FacultyFrame form window should match one that is shown in Figure 7.33 .
Now let ’ s develop the codes for the Insert button click event handler to perform the data insertion function as the project runs. The function of this piece of codes is to insert a new faculty record into our Oracle sample database CSE_DEPT using the Java runtime object method as this button is clicked.
7.4.1.2 Develop the Codes for the Insert Button Event Handler
In fact, there is no difference in the coding part for data insertion to a SQL Server or an Oracle database. You can open the Insert button click event handler from the project SQLSelectObject we built in the last section, copy the codes from that handler, and paste them into our current Insert button click event handler in the project
OracleSelectObject .
Table 7.12. Objects and controls added into the faculty frame window
Type Variable Name Text editable Title
Label Label1 Faculty ID
Text Field FacultyIDField checked
Label Label2 Name
Text Field FacultyNameField checked
Label Label3 Faculty Image
Text Field FacultyImageField checked
To confi rm this data insertion, we can still use the codes inside the Select button click event handler, especially the codes inside the Runtime Object Method block. However, two important modifi cations need to be made to make them our desired validation methods:
1. Modify the codes inside the Select button click event handler to query two more columns, faculty_id and faculty_name , from the Faculty table.
2. Modify the ShowFaculty() method and divide it into two submethods, ShowFaculty() and DisplayImage() .
During the development the codes for the Select button click event handler in Chapter 6 , we only query fi ve columns without including the faculty_id and faculty_
name columns. Now we need to add these two columns for this data insertion validation.
Open the Select button click event handler and perform the modifi cations shown in Figure 7.34 . The modifi ed parts have been highlighted in bold.
Let ’ s have a closer look at this piece of modifi ed codes to see how it works.
A. Two more columns, faculty_id and faculty_name , are added into the faculty text fi eld array f_fi eld since we need to query and display all columns from Faculty table to confi rm the data insertion function.
B. Similarly, these two columns are added into the query string to enable them to be queried.
Now open the ShowFaculty() method and divide this method into two submethods,
ShowFaculty() and DisplayImage() , which are shown in Figure 7.35 . The modifi ed parts have been highlighted in bold.
Figure 7.33. The modifi ed FacultyFrame form window.
c07.indd 505
c07.indd 505 7/20/2011 11:12:08 AM7/20/2011 11:12:08 AM
www.traintelco.com
In Section 7.1.1.4 , we have provided a detailed explanation about the modifi cations for this method, and refer to that section to get more information about this modifi cation.
The purpose of this modifi cation is to allow the new inserted faculty image to be dis- played, either a new faculty image or a default one.
Now we are ready to build and run the project to test the data insertion function.
Click on the Clean and Build Main Project button from the toolbar to build the project. Then click on the Run Main Project button to run the project.
Enter suitable username and password, such as jhenry and test , to the LogIn frame form and select the Faculty Information from the SelectFrame window to open the FacultyFrame form window. Make sure that the Runtime Object Method has been selected from the Query Method combo box. Then click on the Select button to query the default faculty information.
Modify seven text fi elds, which is equivalent to a piece of new faculty information, and enter the default faculty image fi le into the Faculty Image text fi eld, as shown in Figure 7.36 .
Click on the Insert button to try to insert this new faculty record into the Faculty table in our sample database. Immediately, you can fi nd that a debug message is displayed in the Output window, as shown in Figure 7.37 .
Also, you can fi nd that the new inserted faculty name has been added into the Faculty Name combo box if you click on the drop - down arrow from that box.
To confi rm this data insertion, click on the new inserted faculty name from that combo box and click on the Select button to try to retrieve that new inserted faculty record. The validation result will be displayed, and our data insertion action is successful!
To keep our database clean and neat, it is highly recommended to remove this newly inserted faculty record. You can do this data deletion using either the Object Browser in the Oracle Database 10g Express Edition or the Services window in NetBeans IDE 6.8.
Next, let ’ s perform the data updating action against our sample Oracle database using the Java runtime object method.
Figure 7.34. The modifi ed codes for the Select button click event handler.
private void cmdSelectActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
javax.swing.JTextField[] f_field = {FacultyIDField, FacultyNameField, TitleField, OfficeField, PhoneField, CollegeField, EmailField};
String query = "SELECT faculty_id, faculty_name, title, office, phone, college, email " + "FROM Faculty WHERE faculty_name = ?";
if (ComboMethod.getSelectedItem()=="Runtime Object Method"){
try{
DatabaseMetaData dbmd = LogInFrame.con.getMetaData();
String drName = dbmd.getDriverName();
String drVersion = dbmd.getDriverVersion();
msgDlg.setMessage("DriverName is: " + drName + ", Version is: " + drVersion);
//msgDlg.setVisible(true);
PreparedStatement pstmt = LogInFrame.con.prepareStatement(query);
pstmt.setString(1, ComboName.getSelectedItem().toString());
ResultSet rs = pstmt.executeQuery();
A B