This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Return a ResultSet from a model class?

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

 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic