Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:00:46+00:00 2026-06-13T00:00:46+00:00

I need to run several queries in a row Statement st = cnx.createStatement(); ResultSet

  • 0

I need to run several queries in a row

Statement st = cnx.createStatement();
ResultSet rs = st.executeQuery( "SELECT [good stuff]");
// do something smart with rs
rs = st.execute( "SELECT [better stuff]");
// do something smarter with rs
rs = st.execute( "SELECT [best stuff]");
// you got it
try{ rs.close();} catch( SQLException ignore){};
try{ st.close();} catch( SQLException ignore){};

Is this a problem that the first two ResultSet are not properly closed or is it implicitely done during garbage collection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:00:47+00:00Added an answer on June 13, 2026 at 12:00 am

    As soon as you execute the 2nd query, the previous ResultSet is automatically closed. And as far as Garbage Collection is concerned, you don’t have to worry about that. You can just have a stmt.close() at the end that’s all. It will automatically close all the related ResultSet objects.

    Take a look at : – ResultSet#close documentation, which says that: –

    A ResultSet object is automatically closed by the Statement object
    that generated it when that Statement object is closed, re-executed,
    or is used to retrieve the next result from a sequence of multiple
    results.

    If you want to test, whether your resultset gets closed or not, you can use a while loop to iterate over the result set and inside the while loop, create another query and assign it to same result set. You will see that an Exception will be thrown..

    ResultSet res = stmt.executeQuery("SELECT * FROM sometable");
    
    while (res.next()) {
        res.getString(1);
    
        // Closes the previous `ResultSet`
        res = stmt.executeQuery("SELECT * FROM othertable");
    } 
    

    So, in the above code, on the 2nd iteration, you will get an Exception: - Cannot perform operation after ResultSet is closed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For reasons that are irrelevant to this question I'll need to run several SQLite
I have a large table with a multi-part index. I need to run several
I have a couple of queries that run very slowly (several minutes) with the
I need some performance improvement guidance, my query takes several seconds to run and
I need to run several functions at the same time. I had successfully implemented
I'm developing some code that is simulating network equipment. I need to run several
For a personal project I need to run several machine learning algorithms against different
I have several JavaScript functions that need to run when the page loads to
In my iPhone app I would like to run several queries when the application
I need to run several methods after sending file to a user for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.