aspose file tools*
The moose likes JSP and the fly likes Sorting a ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Sorting a ResultSet" Watch "Sorting a ResultSet" New topic
Author

Sorting a ResultSet

Martin Moraga
Greenhorn

Joined: Sep 15, 2008
Posts: 2
Hello,
I have seen a similar post to this one but did not see any answer.

From a JSP page, I run a method in a Java class to query a database and get a resultset.
Back in the JSP I do a loop thru the RS and print every column there is in each row.

Every column can be sorted asc/desc by clicking the column heading. We do not want to access the database every time we click on a column to create again the RS sorted by the column chosen.

What I need is a way to sort the result set by the specific column either asc/desc ONCE CREATED. I have seen I could put the RS into a List and do Collections.sort() but if anybody can give me a real example of this I would really appreciate it.

Thank you all very much for any help
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61227
    
  66

Result sets should never get anywhere near a JSP.

Store the data in a sortable collection at the lowest level possible. You can cache that collection someplace convenient if you don;t want to have to go back to the DB for sorting.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Martin Moraga
Greenhorn

Joined: Sep 15, 2008
Posts: 2
Thanks for your response.
However I did not ask to know if I had to use a ResultSet with a JSP or not. Also, your answer is not clear in what needs to be done.

You mention about going back to the DB for sorting if I do not do "caching at the lowest level possible" but that is precisely what I do not want to do.

Nevertheless I appreciate it if that is all the help you can give. I understand how busy we IT people are usually.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61227
    
  66

Originally posted by Martin Moraga:
However I did not ask to know if I had to use a ResultSet with a JSP or not.
JavaRanch is a learning site. So whether you ask for it or not, you are going to get recommendations regarding best practices. Most people learn by listening to those that have already made the mistakes and share their tales of how to avoid them.

Besides, it's part of the answer. Result sets are cranky, cantankerous beasts and you should copy the data out of them and send them on their way immediately.

Once you have the data in a more reasonable construct, it's a lot easier to do things like caching and sorting and other stuff. To include, accessing them in a reasonable manner in a JSP.
[ September 29, 2008: Message edited by: Bear Bibeault ]
Afzal Hossain
Greenhorn

Joined: Jan 02, 2006
Posts: 25
Hi there, Please make sure to implements Comparable or Comparator interface before trying to sort. Just letting you know in case you forget. Thanks


Afzal
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
Create a simple POJO to hold the data-


Populate the POJO in DAO layer-


List<MyData> can be stored in HttpSession or any other relevant place. Now your JSP should display the data from List<MyData> and whenever required do the sorting on this List, never go back to database again.

Hope this helps.


My Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sorting a ResultSet