aspose file tools*
The moose likes JSP and the fly likes Passing a resultSet object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing a resultSet object" Watch "Passing a resultSet object" New topic
Author

Passing a resultSet object

Kiran Baratam
Greenhorn

Joined: Sep 03, 2003
Posts: 27
Hello All
I am working on a projexct which usees servlets and jsps.
My servlet will execute quries and jsps will display the result.
I would like to know whether it is possible to pass a resultSet object form servlet to a jsp and use the resultset object to display??
If so how do i pass it.Can i use settAttribute method in servlet and do a getAttribute???
Regards
Kiran
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
if you look at the servlet api, the setAttribute method of ServletRequest interface accepts always an object. So you can literally put everything that is an object (not primitives).
so yes, you can put your resultset; but then you should think about performance problems. What happens if your resultset contains 10000 rows? would you put it in the request?
[ October 06, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Kiran Baratam
Greenhorn

Joined: Sep 03, 2003
Posts: 27
Hi
Thanks for the reply.
If i dont send the resultSet object and loop through i need to execute the SQL inside jsp page.So i need to make all DB stuff insid jsp.
that means i am not useing it only for view as per J2EE.
As the application is small i am doing it with servlets and jsps only.
Please suggest me if any alternative solution.
Regards
Kiran
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Kiran Baratam:
Hi
Thanks for the reply.
If i dont send the resultSet object and loop through i need to execute the SQL inside jsp page.So i need to make all DB stuff insid jsp.

I thought your ResulSet had already been populated in your servlet, just before putting it in the request.
Anyways, if your application is small, here's my $0.02:
- In your servlet, once you have your ResultSet, iterate over it, create a Bean (with setter/getter), and put that bean in a vector.
So, if you have a table CUSTOMER:
name
address
city
create a bean with properties name, address, city. For every row you iterate create a bean and set the values of your bean. Once you've populated it, put it in a vector.
When you finish iterating, you will have a vector full of beans. Put the vector in the request (or session, you decide).
In your jsp, you will get the vector. In position 0, for instance, you will have a bean and you can use getter methods to print your results.
I hope I didn't confuse you more . There are many solutions, this is just one.
HTH
Neeraj Singhal
Ranch Hand

Joined: Aug 13, 2000
Posts: 49
Hi Andres
What performance enhancement shall we get by
a) creating a vector of beans
and by
2) NOT putting the resultset object
in the request or session object ??
What are the tradeoffs between the two options ??
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Neeraj -
that's a very good question. I based my solution on the fact that Kiran's application is small (I don't know what "small" means for him/her) and not in performance.
If we start thinking more seriously about performance and robustness, we'll have to check different patterns, such as page-by-page iterator.
The book Web Development with JavaServer Pages (manning) has an example just like this one, in which they populate a vector with beans.
I like this solution because it's clean. It has advantages:
- A row is represented by a JavaBean, making it easier to extract data
- your jsp will be cleaner. You minimize java code in your jsp's
If anyone has other solutions, it'd be grat to read them
thx
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
<<Hi Andres
What performance enhancement shall we get by
a) creating a vector of beans
and by
2) NOT putting the resultset object
in the request or session object ??
What are the tradeoffs between the two options ??>>
If you put the result set in a java bean you may close all db resources at that time. If you pass a ResultSet back you must handle closing of the Statement, ResultSet, and the Connection in the calling code. My preference. Also it is more abstract to hide the db interactions, so you could get and merge data from multiple sources (for example 2 different result sets) without changing code without changing the calling code. Also by putting the ResultSet in a bean it allows you to easily cache the data if that is appropriate.
As far as performance impacts of doing this in my applications the differences are negligible. I time how long it takes to create the Java bean in my apps with JAMon. JAMon is a simple but powerful performance tuning API. In this case it would track average time, min time, max time of the time it takes to create and populate the java bean.
To read about and dowload JAMon (one 90 k jar) go: http://www.javaperformancetuning.com/tools/jamon/#DownloadingJAMon
steve - http://www.jamonapi.com - a simple, fast, free, open source java performance tuning api.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing a resultSet object