Link to home
Start Free TrialLog in
Avatar of mvkraju
mvkraju

asked on

how to show limited number of rows in a page

Hi,

    I have a situation where I have chances to get tons of data with my ResultSet for my query.
I wanted to show100 records per page. I wanted to have next and previous links in my page.
If it is the starting page I should not have previous link. If it is my last page I should not have next link.

How to implement this? I never did this before. I appreciate if could give me ideas.

Thanks
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

you have to get the total number of records and then divide that by 100.. that will give you the number of pages you will have..
when you call your next page you must pass the current page number and then multiply that number by 100 and loop your recordset starting that that number until you reach 100

you get the idea,
Ghost
Avatar of mvkraju
mvkraju

ASKER

It guess it's an exprensive idea, bcz we are Unnecessarily retrieving all the data, no matter wheter user is going to see all the data.
  Say for example my query returned a resultset with 100,000 records, if user see only first 2 pages, in this scenario it's waste of retrieval of 100, 000 records.
 
 
Avatar of TimYates
Have a look at this:

http://jsptags.com/tags/navigation/pager/index.jsp

it may be what you want...
I have a similar application....Try to unedrstand my appliation that displays 100 Records and uses scroller ...for next/previous page.


    int iCounter=0;
    int iPage = 0;
    boolean bIsScroll = true;
    String sSQL="";
    String sPage = "";
    int RecordsPerPage = 100;
  // Build full SQL statement
 
  sSQL = "select n.field1 as n_field from tablename n;
 
  String sNoRecords = "     <tr>\n      <td > NO RECORDS </font></td>\n     </tr>";
  out.println("    <table> ");
     
  try {
    // Select current page
    iPage = Integer.parseInt(getParam( request, "FormNews_Page"));
  }
  catch (NumberFormatException e ) {
    iPage = 0;
  }

  if (iPage == 0) { iPage = 1; }
  RecordsPerPage = 20;
  try {
    java.sql.ResultSet rs = null;
    // Open recordset
    rs = openrs( stat, sSQL);
    iCounter = 0;
    absolute (rs, (iPage-1)*RecordsPerPage+1);
    java.util.Hashtable rsHash = new java.util.Hashtable();
    String[] aFields = getFieldsName( rs );

    // Show main table based on recordset
    while ( (iCounter < RecordsPerPage) && rs.next() ) {

      getRecordToHash( rs, rsHash, aFields );
      String fldfield1 = (String) rsHash.get("n_field1");
 
      out.println("     <tr>");
      // Display the fields here
      out.print("</tr>");
   
      iCounter++;
    }
    if (iCounter == 0) {
      // Recordset is empty
      out.println(sNoRecords);
      bIsScroll = false;
    }

    else {
    // Parse scroller
    boolean bInsert = false;
    boolean bNext = rs.next();
    if ( !bNext && iPage == 1 ) {
   
      out.print("     <tr>\n      <td>\n  ");
      out.println("\n      </td>\n     </tr>");
   
    }
    else {
      out.print("     <tr>\n      <td>");
      bInsert = true;
   
      if ( iPage == 1 ) {
        out.print("\n       <a href_=\"#\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Previous</font></a>");
      }
      else {
        out.print("\n       <a href=\""+sFileName+"?FormNews_Page="+(iPage - 1)+"#Form\"> Previous </a>");
      }
 
      out.print("\n       [ "+iPage+" ]");
 
      if (!bNext) {
        out.print("\n       <a href_=\"#\"> Next </a><br>");
      }
      else {
        out.print("\n       <a href=\""+sFileName+"?"+"FormNews_Page="+(iPage + 1)+"#Form\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Next</font></a><br>");
      }
   
      if ( ! bInsert ) {
        out.print("     <tr>\n      <td colspan=\"2\" style=\"background-color: #FFFFFF; border-style: inset; border-width: 1\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">");
       }
   
      out.println("</td>\n     </tr>");
    }
 
    }

    if ( rs != null ) rs.close();
    out.println("    </table>");
   
  }
  catch (Exception e) { out.println(e.toString()); }

AND ---->


   /**
  * Absolute function
  */
  public static void absolute(java.sql.ResultSet rs, int row) throws java.sql.SQLException{
    for(int x=1;x<row;x++) rs.next();
  }
instead of :
iPage = Integer.parseInt(getParam( request, "FormNews_Page"));

use this:
iPage = Integer.parseInt(request.getParameter("FormNews_Page"));
Avatar of mvkraju

ASKER

TimYates link was good, but i don't want to implement that tough one. In that link he did not gove proper documentions to make use of those custome tags.
The following link is more helpful for me to understand the concept
http://www-106.ibm.com/developerworks/db2/library/techarticle/0307balani/0307balani.html
then i implement my own logic in order to show 60 records per page.

Thanks for ur support.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial