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

H. First a local integer variable, index, is declared and initialized to zero, and it works as a key number to be inserted into our HashMap object list later to build our HashMap object. Also, an acting HashMap object, sCourse, is generated with a key and a value format; both are String variables.

I. A joint query method is used for the course _ id query operation. As you know, there is no faculty _ name column available in the Course Table, and the only available column in the Course Table is faculty _ id. Therefore for a given faculty name, one needs to perform two queries, one to the Faculty Table and the other to the Course Table, to get all desired course _ id values. The first query is used to get a matching faculty _ id based on the selected faculty name, and the second query gets all match-ing course _ id values based on the faculty _ id. It is more efficient to use this joint query to perform these queries.

J. To make sure of the correctness of this query, first we need to check whether the input faculty name argument is an empty or blank string. If it is, we will not build our HashMap object.
K. Then a try-catch block is used to perform the joint query to get all course _ id values.

L. To pick up all queried course _ id values, a while() loop is used with the next() method as the loop condition to collect all course _ id values. Because only one column is returned for this query, the method getString(1) is used to get that column. The put() method is used to build our HashMap object by filling each course _ id that is obtained from the getString(1) method as a value and each index that is converted to a String as the key.

M. This step is very important in building our real HashMap object, c _ course. A user-defined setter() method, setCourse(), which will be built in the second part of the code, is executed to assign our acting HashMap sCourse object to our real HashMap object, c _ course. The argument of this method is our acting HashMap object, sCourse.

N. Another user-defined setter() method, setFacultyName(), is also executed to make sure to display the selected faculty name in the FacultyNameField on our client Course. jsp page even if that page may be updated due to a refreshed page sent to the server.

O. The catch block is used to monitor and check any possible exception during the query and data operations. If any error happens, we will not build our HashMap object.

Related Posts

Leave a Reply

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