aspose file tools*
The moose likes JDBC and the fly likes returning a result set to a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "returning a result set to a jsp" Watch "returning a result set to a jsp" New topic
Author

returning a result set to a jsp

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I got a bean that when called it retuns a result set. I call this bean in a jsp. I get the result set and then after I use it I have to close it in the actual jsp. Bascially what I got going on, is a jsp that spits out some data, and I click on a link where it can sort the data based on a nother column.
Is there a better way to do this?
Sometimes I get errors that say, "Exhautsed Result Set."
And sometimes I get:
"javax.servlet.ServletException: Closed Connection: next"
Please help...
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
One possibility: Remember that JSP pages are multithreaded. Make sure that the ResultSet and Connection are not fields in the JSP servlet (i.e. don't put the variable inside <%! %> tags.
Passing the ResultSet all the way out to the JSP can cause one additional problem. What if the client closes the connection, or if there is some other network error in between the time you open the connection and the time you close? It's a bad idea to interleave I/O from the database and I/O to the browser in this fashion, unless you are *certain* you can catch all the possible I/O errors.
A better strategy might be to have two beans -- one represents a single row from the ResultSet, and the second which represents the results of your query. The second would have some property which exposes a List of the first type. At some point, you will use the second bean to populate this list (which will presumably need to open and close the database connection). Using this approach, you do not have any ResultSet or Connection objects in your JSP page.
[ January 27, 2003: Message edited by: Michael Zalewski ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Anthony Smith:
I got a bean that when called it retuns a result set

Bad idea.
The purpose of beans is to hide database details from the JSP layer. It is a good practice to separate the layers and hide the details of one from the other.
With this in mind, your bean should return the data from the ResultSet, not the ResultSet itself.
Personally I don't use beans, but the behaviour is the same. Your JSP should call some Business logic layer that must gather the data and perform any data manipulation on it. The Business layer is then responsible for the data from the database and can also manage closing connections etc before returning to the JSP.
(actually, we tend to place a wrapper on the DB operations so that the Business layer is not responsible for any DB operations. You ask for data and either get it or don't get it)
Once the JSP receives the data, it is then responsible for displaying the data. That is its only responsibility.
Originally posted by Michael Zalewski:
The second would have some property which exposes a List of the first type.

Like he said!
Dave
Chetan M
Ranch Hand

Joined: Dec 18, 2002
Posts: 43
Hi Anthony,
As David said...

The purpose of beans is to hide database details from the JSP layer. It is a good practice to separate the layers and hide the details of one from the other.
With this in mind, your bean should return the data from the ResultSet, not the ResultSet itself.
---David

and as Michael said...

A better strategy might be to have two beans
-- one represents a single row from the ResultSet, and the second which represents the results of your query. The second would have some property which exposes a List of the first type.
--- Michael Zalewski

U can combine this and go for a single bean which represent/returns the results of ur query in the form of say a "list".
i.e let the bean get the resultset,results of ur query and convert it to a list.. say list of map.. where each item of list contains a map representing one row of data , map holds each field value with field name as key.
U can take this list to ur jsp and play around with it as many no of times.. Actually.. resultset is a view of selected from the db..So as long as resultset is been accessed..connection has to be open..
So try out with this apporach. Hope both david and micheal agree with me.
--- Chetan M
[ January 27, 2003: Message edited by: Chetan M ]

No matter how much you know today, you will have to know more tomorrow.<br /> Hakunamatata !!!
Mohammed Ali
Greenhorn

Joined: Feb 02, 2003
Posts: 4
Originally posted by David O'Meara:

actually, we tend to place a wrapper on the DB operations so that the Business layer is not responsible for any DB operations. You ask for data and either get it or don't get it)
Dave

Great info guys, David can you tell me more about placing wrapper on DB operation..
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: returning a result set to a jsp