Update Records from the Course Table Using JavaServer

Một phần của tài liệu practical database aprogramming with java (Trang 556 - 561)

JAVA RUNTIME OBJECTS METHOD

8.5 BUILD JAVA WEB PROJECT TO ACCESS SQL SERVER

8.5.7 Update Records from the Course Table Using JavaServer

In this section, we will discuss how to update a record for the Course table in our sample database using JSF page and Java beans techniques.

We will use the Update button on the CoursePage page to perform this data updat- ing operation.

Recall that in Section 8.5.6.1 , the Update button in the JSF page CoursePage is bound to the Update() method defi ned in the Java managed bean class CourseBean via the

action attribute of that button. Now we can use this bound relationship to perform the course information updating operation against our sample database.

The key point to be noted is that in most real applications, all pieces of course infor- mation should be updated except the course_id, since it is much easier to insert a new course record with a new course_id than updating a record with an updated course_id .

Figure 8.104. The queried results of the CoursePage.

c08.indd 682

c08.indd 682 7/20/2011 11:12:33 AM7/20/2011 11:12:33 AM

www.traintelco.com

Therefore, in this section, we will concentrate on the updating a course record based on an existing course_id .

As we did for the course information query in the last section, we still want to use the managed bean CourseBean as an intermediate - level controller to call a business method UpdateCourse() defi ned in the session bean CourseFacade to perform this course record updating.

First, let ’ s create the codes for the Update() method in the JSF managed bean CourseBean to call the UpdateCourse() method that is defi ned in the session bean

CourseFacade class and will be developed in the next section to do this course updating.

8.5.7.1 Create Codes for the Update() Method in the JSF Managed Bean Open our project JavaWebDBJSPSQL from the Projects window and our managed bean class CourseBean . Browse to the Update() method and enter the codes that are shown in Figure 8.105 into this method.

Let ’ s have a closer look at this piece of codes to see how it works.

A. In order to perform a course record updating, we need to collect and pass six pieces of updated course information into the UpdateCourse() method defi ned in the session bean

CourseFacade to access the database to do this updating. So a string array newCourse[]

that is used to hold those pieces of information with six elements is created fi rst.

B. Then six pieces of collected course updating information are assigned to each element in the newCourse[] array using the getter methods. The point to be noted is the data process performed between the JSF page CoursePage and the JSF managed bean CourseBean. In fact, before the Update button in the CoursePage can be clicked by the user, all six pieces of course updating information should have been entered into six inputText fi eld. Recall that the value attributes of these six inputText fi elds have been bound to the associated properties defi ned in the CourseBean class. The true story is that as the user clicks the

Update button, all six pieces of course updating information stored in the six inputText fi elds will be submitted to the JSF managed bean CourseBean and assigned to the associ- ated properties defi ned in that bean class. To get those properties, we need to use the associated getter method to do that and assign each of them to an element in the string array newCourse[] . The selectedItem property contains the course_id selected by the user from the <h:selectOneListbox > tag in the CoursePage.

Figure 8.105. The codes for the Update() method.

public Boolean Update() {

String[] newCourse = {null, null, null, null, null, null};

newCourse[0] = selectedItem;

newCourse[1] = getCourseName();

newCourse[2] = getSchedule();

newCourse[3] = getClassroom();

newCourse[4] = getCredit();

newCourse[5] = getEnrollment();

courseFacade.UpdateCourse(newCourse);

return null;

} A B

C D

C. The UpdateCourse() method defi ned in the session bean class CourseFacade is called with the newCourse[] array as the argument to perform this data updating action.

D. The returning value is not important to us since the data updating action has been per- formed by executing the UpdateCourse() method. To simplify the execution of this method, we did not use any returned value to check and confi rm this action. You can use a returned integer or Boolean to do this confi rmation if you like.

Next, let ’ s develop the codes for the UpdateCourse() method in the session bean class to access our sample database to perform this data updating action.

8.5.7.2 Create Codes for the UpdateCourse() Method in the Session Bean Open the code window of the session bean class CourseFacade and create a new method

UpdateCourse() and enter the codes that are shown in Figure 8.106 into this method.

Let ’ s have a closer look at this piece of codes to see how it works.

A. A dynamic JPA query statement with the positional parameters and named parameter is created fi rst. The fi ve pieces of course updating information are arranged in a sequence order with the attached number as the indicator, and the last parameter is a named param- eter CourseID that works as a query criterion.

B. The EntityManager is fi rst cleaned up to make this data updating ready.

C. The dynamic query object is created by calling the createQuery() method.

D. Five pieces of course updating information, including the query criterion course_id , are assigned to the associated positional and named parameters, and one by one defi ned in the query statement. Two points to be noted for these assignments are: fi rst, the order of this assignment must be identical with the order of positional parameters defi ned in the query statement we created in step A . Second, the data types for parameters 4 and 5, or credit and enrollment , are both INTEGER ( SMALLINT for credit and INTEGER for enroll- ment ) when we created the Course table in our sample database. Therefore, you must use the parseInt() method defi ned in the Integer class to convert these two parameters from String to Integer, respectively, and then assign them to those dynamic parameters. Otherwise, you may encounter a compiling error during building the project later.

Figure 8.106. The codes for the UpdateCourse() method.

public void UpdateCourse(String[] nCourse) {

String query = "UPDATE Course c SET c.course=?1, c.schedule=?2, c.classroom=?3, " + "c.credit=?4, c.enrollment=?5 WHERE c.courseId=:CourseID";

em.clear();

Query cQuery = em.createQuery(query);

cQuery.setParameter(1, nCourse[1]);

cQuery.setParameter(2, nCourse[2]);

cQuery.setParameter(3, nCourse[3]);

cQuery.setParameter(4, Integer.parseInt(nCourse[4]));

cQuery.setParameter(5, Integer.parseInt(nCourse[5]));

cQuery.setParameter("CourseID", nCourse[0]);

cQuery.executeUpdate();

} A B C D

E

c08.indd 684

c08.indd 684 7/20/2011 11:12:34 AM7/20/2011 11:12:34 AM

www.traintelco.com

E. Finally the executeUpdate() method is executed to perform this data updating action.

One point to be noted is that no Transaction object should be used for this data manipula- tion since JPA can handle this automatically.

At this point, we have completed all coding jobs for the course data updating opera- tion. Now, let ’ s build and run our project to test this data updating function.

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 CSE - 432 , from the CourseList box, and click on the Details button to get the details for this course. The running result is shown in Figure 8.107 .

To update this course, enter the updating information shown in Figure 8.108 into the fi ve inputText fi elds.

Click on the Update button to try to update this course.

To confi rm this data updating action, two methods can be used: fi rst, you can open the Course table from the NetBeans IDE environment to do this confi rmation. To do that, open the Services window and expand to our Course table, right click on the Course table, and select the View Data item from the pop - up menu to open this table. Browse down to the course CSE - 432 , and you can fi nd that the course has been updated, as shown in Figure 8.109 .

The second way to confi rm this updating action is to use the Details button to try to retrieve the details of this updated course. To do that, fi rst select another course_id from the CourseList box and click on the Details button to get details for that course. Then select the CSE - 432 from the list and click on the Details button again, You can fi nd that the course CSE - 432 has been updated based on the retrieved and displayed details for this course.

Figure 8.107. The running status of the CoursePage.

Before closing this project, it is highly recommended to recover this updated course.

To do that recovery, just enter the following original details for the course CSE - 432 into the fi ve inputText fi elds, and then click on the Update button to complete this data recovery.

Figure 8.109. The updated course CSE - 432.

CourseID: CSE - 432

Course Name: Analog Circuit Designs Schedule: M - W - F: 2:00 – 2:55 PM Classroom: TC - 309

Credits: 3

Enrollment: 18

Figure 8.108. The updating information for the course CSE - 432.

c08.indd 686

c08.indd 686 7/20/2011 11:12:34 AM7/20/2011 11:12:34 AM

www.traintelco.com

Now close the project by clicking on the Close button located at the upper - right corner of this page. Our data updating action using the JSF pages and Java bean is successful.

Finally, let ’ s take care of the course data deleting action against our sample database using the JSF pages and Java bean techniques.

Một phần của tài liệu practical database aprogramming with java (Trang 556 - 561)

Tải bản đầy đủ (PDF)

(791 trang)