Two Laptop Bag*
The moose likes Servlets and the fly likes Return a ResultSet from a model class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Return a ResultSet from a model class?" Watch "Return a ResultSet from a model class?" New topic
Author

Return a ResultSet from a model class?

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Hi,

Is it possible to return a ResultSet from a model class? My code isn't working correctly, and I am unable to find any documentation online to see if this is possible. I would have thought it would have been since it is just another object, so im guessing my syntax is incorrect.

Servlet that calls the model class (all correct import statements are at top of file, not shown in code below)



Code in the model class



When I try to compile I get the following error

"Cannot find symbol"
"Symbol: variable results"
( ie, line 43. )

My guess is that the reason it is throwing an error is due to the results variable being declared within a try/catch block (which could potentially fail).

Although since the ResultSet class is abstract it cannot be created like "ResultSet results = new ResultsSet();" so I am unsure how to fix this issue.

Any ideas?

The point of this model class is as the name suggests, to pull a set of results from the DB to the main servlet class so I can do some looping through the results, then in future so I can easily select data from the DB using this class and likely do similar things.

If this is not possible I guess I will have to create a new method to actually do the work I require but it is a bit sloppy code since there is a lot of duplication.

Any help appreciated.

Thanks

Michael

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

While it is possible, it is highly (and I mean thoroughly and HIGHLY) discouraged. A resultSet holds onto precious database resouces. Best practice is to copy whatever data you need out of the result set and close it as quickly as possible, freeing up all associated DB resources. Then return the copied data.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Ok great info, thanks for tips on best practices :-)

What would you recommend copying this information into so I can return that instead?

Since a HashMap is only a name/value pair, is there something similar to that, but allows several entries (like the rows of the data) ?

Thanks
Michael
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

That really depends upon the nature of the data and how it will be used. I've done everything from a List of beans which each represent the row data, to a simple Object[][]. There are countless other possibilities.

I'd look at it from the point of view of the code that's asking for the data. What makes the most sense for it?
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Had a good think about this and decided to go down the route of a 2d ArrayList which acts as a column/row structure.

Then I can easily loop through the ResultSet and set the first row to the column headings, then any subsequent rows as required for each row returned.

Think I have just about got the 2d ArrayList basics in my head and implemented in my code. Now time to make it more robust and advanced :-D

Thanks for info

Michael
 
Consider Paul's rocket mass heater.
 
subject: Return a ResultSet from a model class?