Bookmark Topic Watch Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Pagination

How do I limit the number of rows displayed in the results page?

Often a database query will yield hundreds or thousands of rows of data.
Trying to display all of them in one webpage could be very slow and resource intensive. Worse, it results in a poor user experience for the person who actually has to scroll through all of this data.

Well-designed websites will return a small subset of the data and provide links to move to the next or last set.

The easiest way to accomplish this is by constraining the number of rows returned from the database query in the first place.


  • MySQL uses the 'LIMIT' keyword for this.
  • PostgreSQL uses the 'LIMIT' and 'OFFSET' keywords for this.
  • Oracle uses the 'ROWNUM' keyword. It doesn't work like LIMIT and OFFSET. You will probably find it strange (this page might help).
  • DB2 uses the 'ROWNUMBER' 'OVER' combination.
  • SQL Server uses the 'TOP' keyword, followed by the number of rows from the top of the result.


  • A typical query in MySQL would look something like:



    This will yield rows 11 through 60.

    (Ref : http://dev.mysql.com/doc/refman/5.0/en/select.html)


    Another way to limit the number of records returned is to use the


    Because this is part of the JDBC API, it works for all servers.

    It should be noted that with both approaches it is undefined which rows are returned. If you want particular rows returned, you should add an "order ..." clause so that the order of rows is defined.


    Javaranch has a forum dedicated to JDBC/SQL issues where you can go to read more about formulating SQL statements: JDBC forum

    JavaRanch Journal article on paging with JDBC and JSP


    Here are some notes from other conversations on pagination:



    Back to the JspFaq or the JdbcUsageQuestions
     
    Your mind is under my control .... your will is now mine .... read this tiny ad
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
      Bookmark Topic Watch Topic
    • New Topic