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 8193089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:21:41+00:00 2026-06-07T04:21:41+00:00

If I do something like try ( Connection conn = Database.getConnection(); PreparedStatement ps =

  • 0

If I do something like

    try (
        Connection conn = Database.getConnection();
        PreparedStatement ps = conn.prepareStatement("SELECT * FROM table WHERE something = ? LIMIT 1");
    ) {
        ps.setString(1, "hello world");
        ResultSet results = ps.executeQuery();
        if(results.next()) {
            // blah
        }
    } catch(SQLException e) {
        e.printStackTrace();
    }

Will the ResultSet still be closed when the PreparedStatement is closed, or will I still have to explicitly close the ResultSet also?

  • 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-07T04:21:42+00:00Added an answer on June 7, 2026 at 4:21 am

    As per javax.sql.Statement.close() method’s JavaDoc:

    Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed.

    So, answering your question – yes, ResultSet will be automatically closed in your case, because related Statement is closed in try-with-resources block.

    However, please note that explicitly closing ResultSets is a good practice which is recommended to follow, so your modified code following good practices would look like:

    try (
        Connection conn = Database.getConnection();
        PreparedStatement ps = prepareStatement(conn, "SELECT * FROM table WHERE something = ? LIMIT 1", param);
        ResultSet results = ps.executeQuery();
    ) {        
        if(results.next()) {
            // blah
        }
    } catch(SQLException e) {
        e.printStackTrace();
    }
    
    private static PreparedStatement prepareStatement(Connection connection, String sql, String param) throws SQLException {
        final PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, param);
        return ps;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to search something like C&A the only query I get in
I want to try create something like Zend's Server Pagecache. What I want to
I am using something like the above sample code but when i try to
I want to do something like this... try { # Something in this function
I'm used to Java so when i try to so something like this: function
I’m getting an error when I try to do something like this below, <%
For example, I try to do something like this: - (BOOL)compare:(NSDecimal)leftOperand greaterThan:(NSDecimal)rightOperand { BOOL
I'm new to Web programming! I try to make an app (something like Client
There is something id really like to probably try in an app but would
String url = getUrl(); try{ Connection con = getConnection(url, username, pwd); }catch(ConnectionException e){ cleanUpUrl(url);

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.