views:

50

answers:

3

Hello All,

I am populating a table in my jsp which has more than 20 records. Now, If I want to display each 10 records in a page and then the user clicks on next link to go to the other 20 records.

How can I do that in my jsp?

This is my current code

<table>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>type</td>
    </tr>
<%
    while(resultSet.next())
    {
        %>
        <tr>
        <td><%=resultSet.getString(1)%></td>
        <td><%=resultSet.getString(2)%></td>
        <td><%=resultSet.getString(3)%></td>
    </tr>
<%
    }
%>
</table>
A: 

What you want is a pagination code example. Here's one I found online: http://www.easywayserver.com/blog/jsp-pagination/ There are lots of examples of this and you'll have to find which one formats your nav links the way you like them

Dylan West
Hello my friend, really it is helpful, but I am using oracle 10 g DB and there are some functions which are not supported like SELECT SQL_CALC_FOUND_ROWS Can you please help
maas
if you need to find the number of rows you can use a stmt like this:select count(*) from table_name where column_name = "something";
Dylan West
I tried it, but failed
maas
What did you use for the table_name and for the column_name? And did you try this query directly in Oracle to see what it returns before trying it in the webpage?
Dylan West
+1  A: 
adardesign
Hello my friend,I need a simpler tutorial, if you can find. I am a little bit beginner
maas
See updated Answer.
adardesign
+1  A: 

You can use 'display' tag which provides pagination, sorting, exporting. In your current code, you are iterating a result set and creating table rows. Having Java code in JSP is not the best practice. Move that into a servlet and create a collection of beans by iterating the result set and set the collection in request scope or session scope(If you want to avoid multiple DB hits).

Sample Usage (From display tag site)

<display:table name="sessionScope.test" pagesize="10">
    <display:column property="id" title="ID" />
    <display:column property="name" />
    <display:column property="email" />
    <display:column property="status" />
</display:table>

Here the 'test' is the name of the attribute that stores a collection. The 'property' attributes for column tag represent the properties of your bean contained in the collection. 'pagesize' specifies the pagination size that the number of items to be displayed in a page.

For more info: http://displaytag.sourceforge.net/1.2/index.html

Marimuthu Madasamy