aspose file tools*
The moose likes Servlets and the fly likes How to order my DB results using servlets?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to order my DB results using servlets??" Watch "How to order my DB results using servlets??" New topic
Author

How to order my DB results using servlets??

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi ranchers: here's the problem:
I perform a database connection on my servlets, retrieve some records, create a bean for every record, put it in a vector and then in the session.
In other words, a vector of beans. Now, when I get to the JSP i take the vector and display all the information I need. And it's working ok. The results are not too many, so I discard having problems with big results (thousands).
Now I wanna add a new feature. The user should be able to order the result either by date or price. So my question is: In case the user selects for example, date, should I forward the request again to the servlet, perform a database connection AGAIN using ORDER BY, populate my vector AGAIN and do the same processing that I did in the first stage?
I think it's a waste of time but can't find any other solution, knowing that I have all the results in my vector in the session.
any suggestions? is this the way to do it?

thanks..


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
If I understand your problem correctly, you can certainly sort your stored beans without having to go back to the database. Use an array of beans instead of a Vector, create a Comparator that works with your beans, and use it with the Arrays class static sort( Object[], Comparator ) method. All in the java.util package.
I would worry about memory problems if your beans are not Serializable.
Bill
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If you have set up the comparator, you can run the static Collections.sort method against the vector.
Collections.sort(myVector)
This article has a brief explanation of using Comparators to sort objects in a Collection:
http://www.javaranch.com/newsletter/July2002/newsletterjuly2002.jsp#collections


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Thomas Paul:
If you have set up the comparator, you can run the static Collections.sort method against the vector.
[/URL]

Against the vector or against the array??
If I set up an array, how do I define its size?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Andres Gonzalez:

Against the vector or against the array??
If I set up an array, how do I define its size?

Collections.sort() can run against any object that implements the List interface (that includes Vector).
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You can get the data out of a Vector as an array of type Object[] by means of the toArray() method, thus automatically getting the right size. See the java.util package API for this and other alternatives.
Either the Collections operation on the original Vector (or ArrayList) or the Array operation on a derived array will work, and should be about the same speed.
Bill
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Guys, it's not sorting . THis is what I've done:
my bean (ProductBean):

Now, in my servlet I do this:

Notice that makeBean is a method that creates a ProductBean with the result from the database. It is added to the vector.
By the way, Thomas, in the article you sent me I think there's a right parenthesis missing ( ) ).

any suggestions..?
thanks
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
This line in the comparator seems a little odd:
return pr1.getItem_price().compareTo(pr2.getItem_price());
Is item_price a String? Because this should not work if price is a float. And the sort order will be odd if the price is a String.

You probably want something more like:
ProductBean pr1 = (ProductBean)o1;
ProductBean pr2 = (ProductBean)o2;
float f1 = pr1.getItem_price();
float f2 = pr2.getItem_price();
if (f1 == f2) return 0;
if (f1 > f2) return 1;
return -1;
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Thank You So Much !@!! ..
another question.. How's the procedure to order by strings, if this
return pr1.getItem_name().compareTo(pr2.getItem_name());
might not work properly??
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The problem isn't with ordering of strings. The problem is ordering numbers when they are stored as strings.
Look at this sequence which would be properly sorted for strings:
12,3456.99
4,5667.21
456.00
71.21
95,213.81
Looks odd but it is in correct string order.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to order my DB results using servlets??