• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pagination problem

 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can some one please help me with pagination problem?

I know how to implement pagination in my jsp pages using arraylist and loop through the resultset.

for (int i = offset.intValue(),l = Math.min(i + maxPageItems, list.size());i < l; i++)

But for one of my application I am getting data from database and storing it into a hash Map using linked list.

I want to implement pagination in jsp page . But I don't know how to do pagination for Collection and is to iterate through each element . Can some one please help me with this.

This is how my code looks right now

List list= null;
HashMap hMap = null;

if(session.getAttribute("RESULT") != null)
{
hMap =(HashMap) session.getAttribute("RESULT");
list = new ArrayList(hMap.keySet());


if(list.size() != 0)
{

for(Iterator iterator = list.iterator();iterator.hasNext();)
{
if(iterator.hasNext())
{
String name = (String)iterator.next();
LinkedList result1 = (LinkedList)hMap.get(name);

DataBean db = (DataBean)result1.get(0);

for (int i=0; i<result1.size() ; i++)
{
db = (DataBean)result1.get(i);


}
}
}
}
}


Gurus please help me.

Thanks,
Maria
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please some one help me
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a firm believer that paginination is something that should be handled at the database level. Why pass a bunch of data to the presentation layer that you aren't even going to show?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use some ORM tool like hibernate for pagination in the data access

object and send only some predefined number of rows from dao like first 20

rows like that.
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

But I need all the data in presentaion layer. Only reason I am using pagination is because I can't display all for Ex: 10000 rows on single page and I don't want to go to servlet every time to get next set of data.

Naseem,
I am not familiar with this ORM tool for pagination in the data access. Can you please give me some example on how to do that.

Thanks,
Maria
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maria Smith:

But I need all the data in presentaion layer. Only reason I am using pagination is because I can't display all for Ex: 10000 rows on single page and I don't want to go to servlet every time to get next set of data.


Why? That's what databases are for. You still haven't explained why you want to do this in such an odd fashion.
[ July 11, 2006: Message edited by: Bear Bibeault ]
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I used before. I don't no any other way of achieving the same result. Please let me know if there is another way of doing this.

Thanks,
Maria
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only ask the database for the slice of data that you need to display on a single page.

The SQL syntax to do this is different for each DB unfortunately. So you'll need to look it up for your DB.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

The SQL syntax to do this is different for each DB unfortunately. So you'll need to look it up for your DB.


Some of the common ones are documented in our JSP FAQ:
http://faq.javaranch.com/view?PaginationOrPaging
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maria,
You can go for JDBC api for that as explained in the url posted by Ben.

Well if you want to do same thing using Hibernate. Then you can se Query or Criterion interface.

You need to create a query using session.



Its a very simple code.

You can find more details of it at Pagination and Pagination code


Naseem
[ July 11, 2006: Message edited by: Naseem Khan ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Switching to Hibernate just to implement simple paging is a mighty big step. And as with all framewroks, one should understand the basics before emplying one.
 
Yuvan Shankar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naseem,
what if we dont have access to DB. Say there is a customer Service which will return the list of customers. And take our requierment like to list all these customer to user using pagination.
So in this case obviously we need to use some collection object and loop through it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an entirely different matter and you should start your own topic if you'd like to pursue it.
 
Stefan Evans
Bartender
Posts: 1743
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A useful method if the List is entirely in memory is the List.subList() method.



You can then just iterate through the displayList, knowing it will produce the right items.
You just have to keep track of the from/to parameters.

There is a tradeoff between database usage, and memory usage here. To me, the solution depends upon the context. If there are 10million rows in the database, I'm not going to load them all into memory, just to display the first 20.
Also if there are hundreds of users, you don't have the memory space to give them their own session variables in memory.

However at the other extreme, if it is an expensive query, returning relatively few records, there can be a case for keeping things in memory (session variables).

Just my 2 cents,
evnafets
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the response.

Maximum rows I am retrieving from the database is 5000 and most of the time it will be less then that
based on selection criteria. So i think i can afford that to keep in memory and display it.
But I am not sure how to create URL link for the list the way it looks in pagination.
Please let me know if you have any ideas.

Thanks,
Maria
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well one idea which comes in my mind is by query string.

In jsp you have no. of links like...

page 1 2 3 4 5 6 and so on



Suppose if a user clicks link 1, then you want to show 1 to 10 rows.

In servlet, retrieve the value of query string which comes as a part of request.




Naseem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic