Can ResultSets be passed between methods of a class? Are there any special usage considerations?

Ryan Breidenbach

Yes. There is no reason that a ResultSet can't be used as a method parameter just like any other object reference. You must ensure that access to the ResultSet is synchronized. This should not be a problem is the ResultSet is a method variable passed as a method parameter - the ResultSet will have method scope and multi-thread access would not be an issue.

Aa an example, say you have several methods that obtain a ResultSet from the same table(s) and same columns, but use different queries. If you want these ResultSets to be processed the same way, you would have another method for that. This could look something like:

public List getStudentsByLastName(String lastName) {
    ResultSet rs = ... (JDBC code to retrieve students by last name);
    return processResultSet(rs);

public List getStudentsByFirstName(String firstName) {
    ResultSet rs = ... (JDBC code to retrieve students by first name);
    return processResultSet(rs);

private List processResultSet(ResultSet rs) {
    List l = ... (code that iterates through ResultSet to build a List of Student objects);
    return l;

Since the ResultSet always has method scope - sychronization is never an issue.

Joe Sam Shirah comments: Ryan's answer is exactly correct in the given context. However, there are other possible areas and concerns as seen by these addtional responses.

swarraj kulkarni notes that: ResultSet does not implement the Serializable interface , so it can not be transferred across the network (for example in RMI calls.)

Brian O'Byrne adds:

  1. There is only one ResultSet. Dont assume that the ResultSet is at the start (or in any good state...) just because you received it as a parameter. Previous operations involving the ResultSet will have had the side-effect of changing its state.
  2. You will need to be careful about the order in which you close the ResultSet and CallableStatement/PreparedStatement/etc.
From my own experience using the Oracle JDBC drivers and CallableStatements the following statements are true:
  • If you close the CallableStatement the ResultSet retrieved from that CallableStatement immediately goes out-of-scope.
  • If you close the ResultSet without reading it fully, you must close the CallableStatement or risk leaking a cursor on the database server.
  • If you close the CallableStatement without reading it's associated ResultSet fully, you risk leaking a cursor on the database server.
No doubt, these observations are valid only for Oracle drivers. Perhaps only for some versions of Oracle drivers.

The recommended sequence seems to be:

  • Open the statement
  • Retrieve the ResultSet from the statement
  • Read what you need from the ResultSet
  • Close the ResultSet
  • Close the Statement

Dieter Wimberger points out an important consideration:
"...a ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results."
[java.sql.ResultSet API Specification]

John Zukowski adds: You have to be sure not to close the connection the result set was acquired from or else the result set will become invalid. You should probably use a CachedRowSet if the connection can/should close.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.