-1

I want to clear the contents of a result set (but nothing should happen to the underlying database) before using it again to execute a new query?

Is there any method which will delete its data (but do nothing to the underlying database, that's important), so that I can use the same resultset again to retrieve data from another query?

A quick search in the API documentation gave me the idea that deleting things through a method like deleteRow() will actually delete data from the database as well. I just want to reset/refresh/clear the resultSet, so that when it is again used to get results of another query, it should contain data from that query only, and not the previous one.

Or does it automatically remove its contents itself once we have retrieved them from it (through methods like ResultSet.getInt(), ResultSet.getString() etc.)

EDIT:- Can we create new result sets for executing each query in the same method?

Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    If you no longer need the values in a resultset object you could use the same resultset object for the next query which would overwrite the old values. – Jay Apr 26 '14 at 21:16
  • 2
    You don't use a resultset to execute a query. A resultset is the result of a query execution. If you want to release the resources associated with a resultset, you must close it, as explained in [the javadoc](http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#close%28%29). – JB Nizet Apr 26 '14 at 21:17
  • @Jay Suppose we retreive values from it into an ArrayList, and the first query returned three values, and the second returned 2 values. The first two values will be over-written, good enough, but what about the third value in the ResultSet (which was from the first query), shall it still be there? – Solace Apr 26 '14 at 21:35
  • @JBNizet That releases the database itself? And now there is another question, when the database is released by a ResultSet, is it also released by the Connection? – Solace Apr 26 '14 at 21:37
  • A database can't be released. That doesn't make any sense. The close() method does what the javadoc says it does. – JB Nizet Apr 26 '14 at 21:38
  • @JBNizet Oh yes, I had misinterpreted this statement from the API: "Releases this ResultSet object's database and JDBC resources immediately..." Thank you for clarifying. – Solace Apr 26 '14 at 21:40

1 Answers1

2

Lets say you want to execute a query like this :

ResultSet rs = statement.executeQuery("select * from people");

You would iterate of the results with something like this:

while(result.next()) {
    // ... get column values from this record
}

Then you just reuse the variable rs to execute another query like:

rs = statement2.executeQuery("select * from cars");

Here is a tutorial on working with ResultSet

geoand
  • 60,071
  • 24
  • 172
  • 190