How do I convert Resultset object to a paginated view on a JSP?
For example, this is my query and result set:
pst = con.prepareStatement("select userName, job, place from contact");
rs = pst.executeQuery();
How do I convert Resultset object to a paginated view on a JSP?
For example, this is my query and result set:
pst = con.prepareStatement("select userName, job, place from contact");
rs = pst.executeQuery();
This Oracle example is wrong.
Yes, in the outer select whe have good ROWNUM values, but it is still pseudo column so we can not use BETWEEN on it. We need one more select.
The right sql code is:
Comrades, using solid sql string and Statement class is SLOOOW. Oracle have to parse your SQL every time your execute it.
Use PreparedStatement and binding parameters.
And fastest solution is put your sql in oracle stored procedure and use CallableStatement to call it.
Here's a couple things you can do:
Determine the total number of pages based on the count of the items.
Display your items based on the offset that you determined (only display starting at item 48)
=======
That's your basic approach. You can tweak this with:
You can use displaytag for paigination or resultset but u download some jar file from displattag
first you create one servlet StudentList.java
{ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
// dao method
Look up the Value List Pattern, and apply that. That's typically the best way to handle these kinds of things.
To start, you need to add one or two extra request parameters to the JSP:
firstrow
and (optionally)rowcount
. Therowcount
can also be left away and definied entirely in the server side.Then add a bunch of paging buttons to the JSP: the next button should instruct the
Servlet
to increment the value offirstrow
with the value ofrowcount
. The previous button should obviously decrement the value offirstrow
with the value ofrowcount
. Don't forget to handle negative values and overflows correctly! You can do it with help ofSELECT count(id)
.Then fire a specific SQL query to retrieve a sublist of the results. The exact SQL syntax however depends on the DB used. In MySQL and PostgreSQL it is easy with
LIMIT
andOFFSET
clauses:In Oracle you need a subquery with
rownum
clause which should look like:In DB2 you need the OLAP function
row_number()
for this:I don't do MSSQL, but it's syntactically similar to DB2. Also see this topic.
Finally just present the sublist in the JSP page the usual way with JSTL
c:forEach
.Note that some may suggest that you need to
SELECT
the entire table and save theList<Contact>
in the session scope and make use ofList#subList()
to paginate. But this is far from memory-efficient with thousands rows and multiple concurrent users.For ones who are interested in similar answer in JSF/MySQL context using
h:dataTable
component, you may find this article useful. It also contains some useful language-agnostic maths to get the "Google-like" pagination nicely to work.