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)
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.
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.
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.
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?
Joined: Sep 30, 2009
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