• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting a ResultSet

 
Martin Moraga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Moraga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, Please make sure to implements Comparable or Comparator interface before trying to sort. Just letting you know in case you forget. Thanks
 
Vinod K Singh
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic