I have this issue: first, I execute a SQL query using Java and then the result is stored into a resultSet() called rs. The query is as follows:
"select ad_id, publisher_id, date, impressions, clicks from table"
Then I read all the data using:
while(rs.next()){
(...)
}
The problem is that the code we are running needs to re-use all information from rs several times (running stochastic gradient so there are several iterations) but when the it starts a new iteration (going from iteration 1 to 2) rs now is null and we have to execute the SQL query once again which takes quite a long time…
Is there some “direct” way of saving the result from this query into a variable? We considered creating two ArrayLists to save the Integers numbers (ad_id, publisher_id…) and one for the date field and then iterate over those lists, but we thought maybe there´s a better and easier way of doing so
In python all we had to do is something like:
data = cursor.fetchall()
and the variable “data” could be used several times, maybe there´s something similar in Java (I´m still learning Java so I´m not sure)
Thanks for any help
If your JDBC ResultSet is scrollable (introduced in JDBC v2 I think), you can reuse the ResultSet itself by calling
ResultSet#beforeFirst, rather than having to copy the data out of it. Example:I have gotten this to work with the MySQL JDBC driver. I tried it on the PostgreSQL driver and that one does not support Scrollable ResultSets, so if it’s not supported by your driver, you’ll need to cache the results in some Collection as the other answers here describe.
If it is scrollable, you can also look at the
absoluteandrelativemethods in the link above to jump to arbitrary points in the ResultSet, which may be useful in some scenarios.Update: As pointed out by Will in the comment below – you may need to additional params in your
createStatementto make the ResultSet scrollable. I’ve added that as the first line of code above.