Java Reference
In-Depth Information
<OPTION VALUE="Acura">Acura</OPTION>
<OPTION VALUE="Audi">Audi</OPTION>
<OPTION VALUE="BMW">BMW</OPTION>
</SELECT>
</TD>
</TR>
Notice also the use of the TOP 50 clause in the stored procedure of Listing 15-1 . The TOP 50 clause
is used to limit the number of hits the search returns. If a large database is searched for common criteria,
you will get a lot of hits, which means big ResultSets using lots of memory. In all probability, your
users won't scroll through more than a few pages before tightening up the search criteria, so giving
them a huge ResultSet is a waste of resources.
If you return the ResultSet to a JavaBean, and make it scrollable, it will be easy to create pages of,
say, five database items per page that the user can scroll. You can always offer the user the option of
requesting additional blocks of 50 results based on the original search criteria by ordering the search on
the primary key and specifying that subsequent ResultSets have higher primary key values.
The search form shown in Figure 15-1 calls a simple JSP page, ProcessSearchForm.jsp , which
uses a JavaBean to handle the query and return the results. As you can see from Listing 15-2 , the JSP
page is very simple. It loads the bean, set its properties, and calls the
SearchFormBean.getMatches() method, which executes the query. It then forwards the user to
SearchFormResultsPage.jsp , which displays the search results.
Listing 15-2: JSP page that loads a JavaBean to query the database
<%@ page language="java"%>
<jsp:useBean id="SearchFormBean"
class="JavaDatabaseBible.ch15.SearchFormBean" scope="session"/>
<jsp:setProperty name="SearchFormBean" property="*"/>
<%SearchFormBean.getMatches();%>
<jsp:forward page="SearchFormResultsPage.jsp"/>
The SearchFormBean itself is shown in Listing 15-3 .
Listing 15-3: JavaBean to handle database query from a JSP page
package JavaDatabaseBible.ch15;
import java.sql.*;
import javax.sql.*;
public class SearchFormBean extends java.lang.Object{
private static String dbUserName = "sa";
private static String dbPassword = "dba";
protected int price;
protected int year;
Search WWH ::




Custom Search