Create the Java Bean or Java Model File, CourseQuery.java – Develop Java Web Applications to Access Databases

8.6.3  Create the Java Bean or Java Model File, CourseQuery.java

To create our Java help class file, CourseQuery.java, to handle the course record query, right-click on our project, JavaWebOracleCourse, in the Projects window and select the New > Java Class item from the popup menu to open the New Java Class wizard. Enter CourseQuery in the Class Name field and select JavaWebOracleSelectPackage from the Package combo box. Click on the Finish button to create the new Java help class file.

Now let’s develop the code for the new Java Bean class file. Double-click on our new created Java Bean class file, CourseQuery.java, in the Projects window to open this file, and enter the code shown in Figure 8.101 into this file. Because of the large size of this piece of code, we divide this coding process into two parts. The first part is shown in Figure 8.101, and the second part is shown in Figure 8.102. The new entered code is in bold.

Let’s have a close look at the new added code in Figure 8.101 to see how it works.

A. A group of libraries and a package are imported first, since all Oracle database-related classes and data operation methods are defined in those libraries and packages. A java.util. HashMap library is also imported here, since we need some classes defined in that library to build our HashMap object list, c _ course.
B. Seven local properties related to the associated columns in the Course and Faculty Tables in our sample database are declared first. These properties are very important, since they are directly mapped to the associated columns in those Tables. All of these properties can be accessed by using the associated getter() method defined at the bottom of this class.
C. A public HashMap object, c _ course, is declared and initialized to null, and we will build this object by filling in all queried course _ id values to this list later. In fact, this object can be defined as a private object and can be accessed by calling a getter() method, get _ CourseID(), defined in the second part of the code for this class. Here we just define it as public to make it easy to access.

D. A static or a class level connection object, con, is defined here to let it be accessed by all methods defined in this class file.
E. Inside the constructor of this class, a database connection process is executed to connect to our sample database, and the connection object is stored in the class-level object, con.
F. A user-defined method, CloseDBConnection(), is defined, and it is used to close the database connection if all data queries are completed.

G. Another user-defined method, getCourse(), is defined here with a faculty name as the input argument. The function of this method is to perform a database query to retrieve all matching course _ id values based on a selected faculty member and fill those course _ id val-ues into our HashMap object list, c _ course, which will be used by our Controller Course Process.jsp to display them in the CourseList box located at our client Course.jsp later.

FIGURE 8.101   The first part of the code for the Java Bean class CourseQuery.java.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *