• 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

returning a result set to a jsp

 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
My cellmate was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic