JAVA RUNTIME OBJECTS METHOD
8.5 BUILD JAVA WEB PROJECT TO ACCESS SQL SERVER
8.5.8 Delete Records from the Course Table Using JavaServer
As we did for the course data updating action in the last section, we still want to use the
Delete button defi ned in the CoursePage and the associated Delete() method defi ned in the managed bean CourseBean to perform this data deletion action.
First, let ’ s build the codes for the Delete() method in our managed bean CourseBean class.
8.5.8.1 Build Codes for the Delete() Method in the JSF Managed Bean
Open the code window of the managed bean CourseBean and browse to the Delete() method, and enter the codes shown in Figure 8.110 into this method.
Let ’ s have a closer look at this piece of codes to see how it works.
A. First, a returned boolean variable delete is created and initialized to false.
B. Then the DeleteCourse() method that is defi ned in the session bean CourseFacade and will be developed in the next section is called with the course_id as the argument to perform this course data deleting action.
C. If the returned value is false , which means that this action has failed, a warning message is displayed using our JDialog box to indicate this situation.
D. Otherwise, the course data deletion is successful. In fact, the returned value is not important to us and therefore a null is returned.
Now let ’ s build the codes for the DeleteCourse() method in our session bean class to perform this data - deleting action.
Figure 8.110. The codes for the Delete() method.
public Boolean Delete() { boolean delete = false;
delete = courseFacade.DeleteCourse(selectedItem);
if (!delete) {
msgDlg.setMessage("The course deleting is failed!");
msgDlg.setVisible(true);
}
return null;
} A B C
D
8.5.8.2 Build Codes for the DeleteCourse() Method in the Session Bean
Open the code window for our session bean class CourseFacade and create a new method DeleteCourse() . Enter the codes shown in Figure 8.111 into this method.
Let ’ s have a closer look at this piece of codes to see how it works.
A. A local integer variable delete is created, and it is used to hold the running result of the execution of the data deleting action.
B. The course deleting statement string is created with a named parameter CourseID . C. The EntityManager is cleaned up to make it ready for this data deleting action.
D. The createQuery() method with the deleting query string as the argument is executed to create this deleting query object cQuery .
E. The setParameter() method is executed to initialize the named parameter CourseID with the input argument cid , which is the selected course_id .
F. The deleting action is performed by calling the executeUpdate() method, and the running result of this deleting action is assigned to the local integer variable delete .
G. The running result of this deletion is exactly an integer number indicating how many rows have been successfully deleted from the Course table. If this returned result is not equal to 0, which means that at least one row has been deleted from the Course table and this deleting action is successful, a true is returned to indicate this situation.
H. Otherwise, the deleting action has failed, and a false is returned.
Now let ’ s build and run the project to test this data deleting action.
Click on the Clean and Build Main Project button to build our project. If everything is fi ne, right click on our CoursePage.jsp from the Projects window and select the Run File item from the pop - up menu to run this page.
Enter a faculty name, such as Jenney King , into the Faculty Name fi eld, and click on the Select button to retrieve and display all courses taught by this faculty in the CourseList box. Then select one course_id , such as CSC - 233B , from the CourseList box, and click on the Delete button to try to delete this course from our Course table.
To confi rm this deleting action, two ways can be used. One way is to open the Course table from the Services window in the NetBeans IDE to check whether this course has
Figure 8.111. The codes for the DeleteCourse() method.
public boolean DeleteCourse(String cid) { int delete = 0;
String query = "DELETE FROM Course c WHERE c.courseId =:CourseID";
em.clear();
Query cQuery = em.createQuery(query);
cQuery.setParameter("CourseID", cid);
delete = cQuery.executeUpdate();
if (delete != 0) return true;
else
return false;
} A B C D E F G H
c08.indd 688
c08.indd 688 7/20/2011 11:12:34 AM7/20/2011 11:12:34 AM
www.traintelco.com
been removed. Another way is to use the Select button to try to retrieve all courses taught by the selected faculty from our Course table to confi rm this deleting action. Now let ’ s use the second way to do this checking since it is easy.
Make sure that the faculty member Jenney King is still in the Faculty Name fi eld, and click on the Select button to try to retrieve all courses taught by this faculty. You can fi nd that no course CSC - 233B is displayed in the CourseList box, as shown in Figure 8.112 , and this course has been deleted from the Course table successfully.
Now close the project by clicking on the Close button located at the upper - right corner of this page. Our data deleting action using the JSF pages and Java bean is successful.
It is highly recommended to recover this deleted course from the Course table to make our database clean and neat. Use the data shown in Table 8.8 to do this recovery job.
One easy way to recover this record is to open the Microsoft SQL Server 2008 Management Studio to insert a new row shown in Table 8.8 into our Course table. On the opened SQL Server 2008 Studio, expand to our dbo.Course table and right click on it, and select the Edit Top 200 Rows item to do this new row insertion.
The codes for the Back button in the CoursePage are not important in this applica- tion, and we ’ d like to leave this coding as a homework to the readers.
A complete Java Web application project JavaWebDBJSPSQL can be found from the folder DBProjects\Chapter 8 that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1). You can download this project and test it in your computer if you like.
Figure 8.112. The running result of deleting course action.
Table 8.8. The course CSC - 233 B record in the Course table
course_id course credit classroom schedule enrollment faculty_id
CSC-233B Introduction to Algorithms 3 TC-302 M-W-F: 11:00-11:55 AM 19 K69880
Next, let ’ s handle building another Web application project to access and manipulate the data against Oracle databases.