• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting a ResultSet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's never done THAT before. Explain it to me tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic