JAVA RUNTIME OBJECTS METHOD
8.6 BUILD JAVA WEB PROJECT TO ACCESS AND MANIPULATE ORACLE DATABASE
8.6.9 Insert New Records to the Faculty Table Using
As we did for the faculty information query action in the last section, we can divide this new faculty insertion action into two Java beans: the Java managed bean FacultyMBean.
java that is used to manage and control the data insertion, and the session bean
FacultySessionBean.java that is used to perform the actual data insertion actions.
First, let ’ s build the codes for the managed bean to manage this data insertion action.
8.6.9.1 Add the Codes to the Java Managed Bean to Manage Data Insertions To begin this coding process, let ’ s fi rst add a new method Insert() into our managed bean
FacultyMBean.java and bind it to the action attribute of the <h:commandButton id =″Insert ″> tag in our JSF page FacultyPage.jsp .
Open the code window of our managed bean FacultyMBean.java and add a new method Insert() into that fi le with the codes that are shown in Figure 8.145 .
Recall that in Section 8.6.8.1 , when we modifi ed our JSF page FacultyPage.jsp fi le, we added one command tag shown below to that page:
<h:commandButton id =″Insert ″ action =″#{FacultyMBean.Insert} ″ value =″Insert ″ / >
Figure 8.144. The running result of the project.
c08.indd 728
c08.indd 728 7/20/2011 11:12:36 AM7/20/2011 11:12:36 AM
www.traintelco.com
With this tag, the Insert() method we just added into our managed bean has been bound to the action attribute of that tag, and this method will be called and executed as soon as the user clicks the Insert button on our JSF page FacultyPage.jsp as the project runs.
Let ’ s have a closer look at this piece of new added codes to see how it works.
A. Some local variables are created fi rst for this method. The insert is a boolean variable used to hold the running status of the InsertFaculty() method defi ned in the session bean class, and we will build this method in the next section. The msgDlg is a JDialog instance used to display the debug or exception information as the project runs. The fInsert[] is a string array used to hold seven pieces of new faculty information to be inserted into the Faculty table in our sample database. The point to be noted is that we used seven properties defi ned in this managed bean as seven pieces of new faculty information, since those properties have been bound to the associated value attributes of the <h:inputText > tags in our JSF page FacultyPage.jsp . Furthermore, those properties can be automatically updated as the users enter seven pieces of new faculty information into seven text fi elds related to those tags in our JSF page FacultyPage.jsp .
B. If the user entered a new faculty image into the Image fi eld in the JSF page, the property
facultyImageName defi ned in the managed bean should contain a valid faculty image fi le ’ s name, and this name is assigned to the property facultyImage that will be used to display this new image later.
C. The InsertFaculty() method defi ned in our session bean, which will be developed in the next section, is called to perform this new faculty record insertion. The argument passed into that method is the string array fInsert[] that contains seven pieces of new faculty information. The running result of that method is returned and assigned to the local vari- able insert .
D. If the running result of the method InsertFaculty() is false, which means that the data insertion has failed, this situation is displayed by executing the setMessage() method in the msgDlg instance.
E. A null is returned since this returning value is not important to this application.
Next, let ’ s develop the InsertFaculty() method in our session bean class
FacultySessionBean.java to perform the data insertion using the Hibernate API.
Figure 8.145. The codes for the new added method Insert().
public String Insert() { boolean insert = false;
MsgDialog msgDlg = new MsgDialog(new javax.swing.JFrame(), true);
String[] fInsert = {facultyID, name, office, phone, college, title, email};
if (facultyImageName != null) facultyImage = facultyImageName;
insert = facultySessionBean.InsertFaculty(fInsert);
if (!insert) {
msgDlg.setMessage("The faculty insertion is failed!");
msgDlg.setVisible(true);
}
return null;
} A
B C D
E
8.6.9.2 Build the InsertFaculty() Method for the Session Bean to Perform Data Insertions
Open the code window for our session bean class FacultySessionBean.java , and enter the codes shown in Figure 8.146 into this fi le to create a new method InsertFaculty() and its codes.
Let ’ s have a closer look at this piece of new codes to see how it works.
A. First, a new instance of the entity class Faculty is created, since we need this object to perform new faculty record insertion later.
B. A new Transaction object tx is created to help to perform this data insertion action.
C. If this new Transaction instance has not been active, the begin() method is executed to begin this transaction instance.
D. Seven setter methods are executed to set up seven pieces of new faculty information to the newly created Faculty entity object.
E. The insertion action is performed by executing the persist() method for the session object with the Faculty entity object as the argument of this method.
F. The commit() method is executed to actually trigger and perform this insertion action.
G. Finally, a true is returned to the calling method to indicate the success of this data insertion.
Now let ’ s build and run the project to test this data insertion function.
8.6.9.3 Run the Project to Test the New Faculty Record Insertion
Click on the Clean and Build Main Project button to build our project. If everything is fi ne, right click on our JSF page FacultyPage.jsp from the Projects window and select the Run File item to run the project. Of course you can run the project by starting from the LogIn page.
Figure 8.146. The codes for the InsertFaculty() method.
public boolean InsertFaculty(String[] newFaculty) { Faculty ft = new Faculty();
org.hibernate.Transaction tx = session.beginTransaction();
if (!tx.isActive()) tx.begin();
ft.setFacultyId(newFaculty[0]);
ft.setFacultyName(newFaculty[1]);
ft.setOffice(newFaculty[2]);
ft.setPhone(newFaculty[3]);
ft.setCollege(newFaculty[4]);
ft.setTitle(newFaculty[5]);
ft.setEmail(newFaculty[6]);
session.persist(ft);
tx.commit();
return true;
} A B C D
E F G
c08.indd 730
c08.indd 730 7/20/2011 11:12:36 AM7/20/2011 11:12:36 AM
www.traintelco.com
On the opened Faculty Page, type a desired faculty name such as Ying Bai into the
Faculty Name fi eld to perform a query for that faculty member. Then enter seven pieces of new faculty information shown in Figure 8.147 into the associated seven fi elds as a new faculty record. Also, enter the default faculty image fi le ’ s name, Default.jpg , into the
Image fi eld as a new image for this new faculty, as shown in Figure 8.147 . Then click on the Insert button to try to insert this new faculty record into the Faculty table in our sample database.
To check and confi rm this new data insertion, open the Faculty table from our sample Oracle database by performing the following operations:
A. Open the Services window and expand the Databases node.
B. Right click on our Oracle database URL: jdbc:oracle:thin:@localhost:1521:XE [CSE_
DEPT on CSE_DEPT] , and select the Connect item to connect to our database.
C. Expand our sample database CSE_DEPT and Tables .
D. Right click on the Faculty table and select the View Data item.
Your opened Faculty table is shown in Figure 8.148 .
It can be found that the new faculty record with the faculty_id of W56789, which is located at the fi rst row and has been highlighted in dark color, has been successfully inserted into our database. Our data insertion action is successful!
It is highly recommended to remove this newly inserted faculty record from our database since we want to keep our database clean. To do this clean up, click and select the fi rst row in this Faculty table, and click on the Delete Selected Record button that is the second button under the query statement: select * from CSE_DEPT.FACULTY at the top of this window. Click on the Yes button to the popup message box to confi rm this deletion action.
Next, let ’ s discuss how to update and delete an existing faculty record in our database using the JSF faces and Java beans.
Figure 8.147. The newly inserted faculty record.