Java Reference
In-Depth Information
cs.setString(3,make);
cs.setString(4,model);
cs.setString(5,engine);
cs.setString(6,transmission);
cs.setInt (7,price);
cs.setInt (8,year);
rs = cs.executeQuery();
rs.last();
rowCount = rs.getRow();
con.close();
}catch(ClassNotFoundException e1){
System.err.println(e1.getMessage());
}catch(SQLException e2){
System.err.println(e2.getMessage());
}
return rowCount;
}
}
In addition to the normal getter and setter methods for its properties, the JavaBean has a number of
methods to retrieve the data and to navigate around the scrollable
ResultSet
. These methods include:
getMatches()
. This method sets the parameters of the SQL stored procedure and calls it to get
the scrollable
ResultSet
. It then gets the row count by navigating to the end of the
ResultSet
and getting the row number using
getRow()
. The
ResultSet
is now stored in the JavaBean for
access by the other methods.
selectRow(int row)
. The
selectRow()
method moves the cursor to the selected row. The
row
argument refers to the row number within the displayed JSP page, and the integer
index
provides the offset to the row corresponding to the beginning of the current JSP page. The
selectRow()
method navigates to the desired row using
ResultSet.absolute(index)
to
move the cursor to the row corresponding to the first row in the displayed JSP page. It then uses
ResultSet.relative(row)
to move to the row corresponding to the relative row within the
displayed JSP page. Finally, it sets the JavaBean's properties by getting the appropriate data from
the
ResultSet
.
pageForward()
. The
pageForward()
method moves the row index to the row corresponding
to the top of the next page.
pageBack()
. The
pageBack()
method moves the row index to the row corresponding to the top
of the previous page.
getRowCount()
. The
getRowCount()
method returns the
rowCount
.
getPage()
. The
getPage()
method returns a String representation of the current page number
in the form "Page
1
of n".
The only slightly tricky logic involved in
Listing 15-3
is in the area of moving the cursor. It is important to
remember that absolute row numbers start at 1 and that relative row numbers can never be zero.
The JSP page used to display the search results is kept separate from the JSP page that instantiates
the bean and executes the query. It includes two HTML form elements containing the page buttons and
calls separate JSP pages to handle navigation through the
ResultSet
. The code for the display page
is shown in
Listing 15-4
.
Listing 15-4:
Search-results page JSP