Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To start, you need to add one or two extra request parameters to the JSP: <code>firstrow</code> and (optionally) <code>rowcount</code>. The <code>rowcount</code> can also be left away and definied entirely in the server side.</p> <p>Then add a bunch of paging buttons to the JSP: the <em>next</em> button should instruct the <code>Servlet</code> to increment the value of <code>firstrow</code> with the value of <code>rowcount</code>. The <em>previous</em> button should obviously decrement the value of <code>firstrow</code> with the value of <code>rowcount</code>. Don't forget to handle negative values and overflows correctly! You can do it with help of <code>SELECT count(id)</code>.</p> <p>Then fire a specific SQL query to retrieve a <strong>sublist</strong> of the results. The exact SQL syntax however depends on the DB used. In MySQL and PostgreSQL it is easy with <code>LIMIT</code> and <code>OFFSET</code> clauses:</p> <pre><code>private static final String SQL_SUBLIST = "SELECT id, username, job, place FROM" + " contact ORDER BY id LIMIT %d OFFSET %d"; public List&lt;Contact&gt; list(int firstrow, int rowcount) { String sql = String.format(SQL_SUBLIST, firstrow, rowcount); // Implement JDBC. return contacts; } </code></pre> <p>In Oracle you need a subquery with <code>rownum</code> clause which should look like:</p> <pre><code>private static final String SQL_SUBLIST = "SELECT id, username, job, place FROM" + " (SELECT id, username, job, place FROM contact ORDER BY id)" + " WHERE ROWNUM BETWEEN %d AND %d"; public List&lt;Contact&gt; list(int firstrow, int rowcount) { String sql = String.format(SQL_SUBLIST, firstrow, firstrow + rowcount); // Implement JDBC. return contacts; } </code></pre> <p>In DB2 you need the OLAP function <code>row_number()</code> for this:</p> <pre><code>private static final String SQL_SUBLIST = "SELECT id, username, job, place FROM" + " (SELECT row_number() OVER (ORDER BY id) AS row, id, username, job, place" + " FROM contact) AS temp WHERE row BETWEEN %d AND %d"; public List&lt;Contact&gt; list(int firstrow, int rowcount) { String sql = String.format(SQL_SUBLIST, firstrow, firstrow + rowcount); // Implement JDBC. return contacts; } </code></pre> <p>I don't do MSSQL, but it's syntactically similar to DB2. Also see <a href="https://stackoverflow.com/questions/971964/limit-10-20-in-sqlserver">this topic</a>.</p> <p>Finally just present the sublist in the JSP page the usual way with JSTL <a href="http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/c/forEach.html" rel="noreferrer"><code>c:forEach</code></a>.</p> <pre><code>&lt;table&gt; &lt;c:forEach items="${contacts}" var="contact"&gt; &lt;tr&gt; &lt;td&gt;${contact.username}&lt;/td&gt; &lt;td&gt;${contact.job}&lt;/td&gt; &lt;td&gt;${contact.place}&lt;/td&gt; &lt;/tr&gt; &lt;/c:forEach&gt; &lt;/table&gt; &lt;form action="yourservlet" method="post"&gt; &lt;input type="hidden" name="firstrow" value="${firstrow}"&gt; &lt;input type="hidden" name="rowcount" value="${rowcount}"&gt; &lt;input type="submit" name="page" value="next"&gt; &lt;input type="submit" name="page" value="previous"&gt; &lt;/form&gt; </code></pre> <p>Note that some <em>may</em> suggest that you need to <code>SELECT</code> the entire table and save the <code>List&lt;Contact&gt;</code> in the session scope and make use of <a href="http://java.sun.com/javase/6/docs/api/java/util/List.html#subList%28int,%20int%29" rel="noreferrer"><code>List#subList()</code></a> to paginate. But this is <em>far</em> from memory-efficient with thousands rows and multiple concurrent users.</p> <p>For ones who are interested in similar answer in JSF/MySQL context using <code>h:dataTable</code> component, you may find <a href="http://balusc.blogspot.com/2008/10/effective-datatable-paging-and-sorting.html" rel="noreferrer">this article</a> useful. It also contains some useful language-agnostic maths to get the "Google-like" pagination nicely to work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload