Build the Help Class FacultyBean – Develop Java Web Applications to Access Databases

8.1.4.3   Build the Help Class FacultyBean

This class is a help class but is very similar to a real Java bean class. The code for this class is shown in Figure 8.15.

FIGURE 8.15   The code for the FacultyBean help class.

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

A. At the beginning of this class, seven member data or properties of this class are defined. This is very important in a Java bean class, since all data-related transactions between the client pages and Java bean are dependent these properties. In other words, all clients could pick up data from a Java bean using those properties, and a one-to-one relationship exists between each property in the Java bean class and each queried column in the data Table.

According to convention, all of these properties should be defined as a private data type and can be accessed by using the getter() methods provided in the Java bean class.
B. A new instance of the Hibernate session class is created and initialized. The point to be noted is that this Hibernate session object is different from the JSP implicit session object. C. The getCurrentSession() method is executed to get the default Hibernate session object. D. The detailed definition of the QueryFaculty() method starts from here with the method header.
E. A new java.util.List instance is created and initialized, since we need this object to pick up and hold our queried faculty result. The MsgDislog instance is used to display error information in case an exception is encountered during this query operation.
F. A try . . . catch block is used to perform our data query. First a new Transaction instance, tx, is created with the beginTransaction() method.
G. Then a query string built with Hibernate Query Language (HQL) is created, and this query string will be used to perform the faculty information query later.
H. The list() method is executed to perform a query to the Faculty Table in our sample database to try to retrieve a matching faculty record based on the selected faculty name fname. The query result is assigned to and held in a local variable, facultyList, that has a List data type.

I. The catch block is used to track and collect any possible exception during the query process. An error message will be displayed if the query encounters any problem.

J. The facultyList.get(0) method is used to retrieve the first matching row from the query result. In fact, only one faculty row should be queried and retrieved, since all fac-ulty names are unique in our sample database. A sequence of getter() methods is used to pick up the associated columns and assign them to the associated properties in the FacultyBean class. Finally, the query result is returned to the FacultyQuery.jsp page.

K. Seven getter() methods are defined at the bottom of this class, and they can be used to pick up all properties defined in this class.

Leave a Comment



                    Blog, Contribute ,Privacy,Terms of Use,Trademarks,© estherknow 2024