|
 |
servlets thread: HELP REQUIRED - DISPLAYING 20 search results per page
Message #1 by "Zubin Wadia" <digital_picasso@h...> on Thu, 31 May 2001 23:11:45
|
|
Thsi is the logic.
Please work it out based on your appln.
Lets assume that you have a total of 46 records.
int x = 0; int y = 0;
x=46/20 -------> 2.
y=46%20 --------> 6.
if(y!=0 && y<=19)(Which will be the case y can never
be 20)
x++;
You now know that you have three pages which means 40
plus records.
You could store the results in a hashtable( to avoid
database hits).
for the first page
for(int z=0;z<20;z++)
{
int count++;
}
this will print you the first 20 records. You can then
pass a parameter to the next page which will retrieve
the next 20 records.
count will be 19. when this is passed as parameter.
increase it by 1. then the loop will continue like
this
for(z=var;z<var+20;z++)
{
count++;
}
this will go on but we always should ensure that we
have a value of total count - count > 20. if it is
less then this would mean that we have less than 20
records to display. Then the loop would change.
for(z=var;var<var+value;var++)
{
}
where var = count+1 at all times.
value = total count - record count(if diff<20)
you can use history.back() property of the browser to
get back to the previous screen.
Hope this helps...
--- Zubin Wadia <digital_picasso@h...> wrote:
> I am using a DB2 database and JSP/Servlets. I need
> to know how one can
> return 20 results a page:
>
> <<Previous 1 2 3 4 5 6 7 8 9 10 Next>>
>
> I really would love to know how this is done! I have
> attached the sample
> servlet that requires this to be done. Other methods
> are welcome. Please
> do help I have been pondering over this long enough!
>
>
> Sincerely,
>
> Zubin R. Wadia
>
> import java.io.*;
> import java.util.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.sql.*;
> import java.lang.*;
> import java.net.*;
> import myDBConnection;
>
> public class cartquick extends HttpServlet
> implements SingleThreadModel{
>
> ResultSet rs;
> ResultSet rs2;
>
> Connection con = null;
> Statement stmt = null;
> Statement stmt2 = null;
> myDBConnection dbConnect;
> String sqlStatement;
> int totalPageCount = 0;
>
> private LogFile logFile;
>
> public void doGet(HttpServletRequest req,
> HttpServletResponse res)
> throws ServletException, IOException {
>
> logFile = new LogFile("cartquick.log",false);
> logFile.initLogFile();
> logFile.writeToLogFile("cartquick : doGet :
> Starting
> cartquick");
>
> ServletOutputStream out = res.getOutputStream();
> out.println("<HTML><HEAD><TITLE>ImageWork
> ObjectStore
> Search Results</TITLE></HEAD>");
> out.println("<BODY>");
> out.println("<br>");
> out.println("<font color=\"#000000\"
> face=\"Arial\"
> size=\"4\"><B>SEARCH RESULTS</B>");
> out.println("<br>");
> out.println("<br>");
>
> out.println("</BODY></HTML>");
>
> try {
>
> dbConnect = new myDBConnection();
> con = dbConnect.getJDBCConnection();
>
> logFile.writeToLogFile("cartquick : doGet :
> JDBCConnection Made to " +
> dbConnect.getJDBCDriverType());
> stmt = con.createStatement();
>
>
> String caption = null;
> String heading = null;
> String fieldname = null;
> String displaylength = "";
> String fieldVal = null;
> String[] fieldnames = new String[100];
>
>
> InetAddress localip = InetAddress.getLocalHost();
> String slocalip = localip.getHostAddress();
>
> out.println("<HTML><HEAD><TITLE>ImageWork
> ObjectStore Search Results</TITLE></HEAD>");
> out.println("<BODY>");
> out.println("<TABLE BORDER='1'>");
> out.println("<TR>");
>
> int i = 1;
>
> logFile.writeToLogFile("cartquick : doGet :
> Building SQL Statement");
> sqlStatement = "SELECT * FROM appcartier where";
> String searchVal = parseVal(req.getParameter
> ("text"));
> if( !searchVal.equals("")){
> sqlStatement += " refnumber
> like '%"+searchVal+"%' or catnumber like
> '%"+searchVal+"%' or sernumber
> like '%"+searchVal+"%'"+
> " or materialid in (select materialid from
> appcartmat where matclass like '%"+searchVal+"%' or
> material
> like '%"+searchVal+"%' or smaterial like
> '%"+searchVal+"%')"+
> " or bdescription like '%"+searchVal+"%'
> and ";
> }
>
> sqlStatement = sqlStatement + " deleteobj = '0'";
> logFile.writeToLogFile("cartquick : doGet : SQL
> sqlstatement = " + sqlStatement);
>
> logFile.writeToLogFile("cartquick : doGet :
> Excuting sql statement");
> rs = stmt.executeQuery(sqlStatement);
> logFile.writeToLogFile("cartquick : doGet :
> Statement executed successfully");
>
>
>
> //Build the table headings
> logFile.writeToLogFile("cartquick : doGet :
> Building Table headings");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>Ref #</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>Serial #</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>Category #</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>Date In
> Stock</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>CAT</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>SCAT</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>SSCAT</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>Material(s)</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>Color</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>Shape</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>Image</b></TH>");
> out.println("<TH><font face=\"Arial\"
> size=\"2\"><b>DETAILS</b></TH>");
>
> out.println("<TD></TD>");
> int rowcount = 0;
> boolean rsNext = true;
> stmt2 = con.createStatement();
> ResultSet catRS;
> Statement catStmt = con.createStatement();
> ResultSet matRS;
> Statement matStmt = con.createStatement();
> String matSql = "";
> String material = "";
> ResultSet colorRS;
> Statement colorStmt = con.createStatement();
> String colorSql = "";
> String color = "";
> ResultSet shapeRS;
> Statement shapeStmt = con.createStatement();
> String shapeSql = "";
> String shape = "";
> String catid = "";
> String cat = "";
> String scat = "";
> String sscat = "";
> int rcount = 0;
> String pNumber ="";
> ResultSet rs3;
> Statement stmt3 = con.createStatement();
> totalPageCount = 0;
> //rowCount = 0;
>
> loop:while (rs.next()) {
> pNumber = rs.getString("pagenumber");
> if(pNumber != null){
> pNumber = pNumber.trim();
> }
> System.out.println("pnumber='"+pNumber+"'");
> if(!pNumber.equals("01")) {
> continue loop;
> }
> out.println("<TR>");
> rowcount = rowcount + 1;
> String rNumber = rs.getString("refnumber");
> if(rNumber != null){
> rNumber = rNumber.trim();
> if(!rNumber.equals("")){
> out.println("<TD><font face=\"Arial\" size=\"2\">" +
>
>
=== message truncated ===
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/
|
|
 |