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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:22:51+00:00 2026-06-13T09:22:51+00:00

When using JDBC in Java, the generally accepted method of querying a database is

  • 0

When using JDBC in Java, the generally accepted method of querying a database is to acquire a connection, create a statement from that connection, and then execute a query from that statement.

// load driver
Connection con = DriverManager.getConnection(..);
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("SELECT..");
// ...

However, I am unsure of how to treat a second query to the same database.

  1. Can another query be executed safely on the same Statement object, or must another statement be created from the Connection object in order to execute another query?

  2. If the same Statement object can be used for multiple queries, what is the purpose of the Statement class (since it would then make more sense for a Connection.executeQuery() method to exist)?

  • 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-13T09:22:52+00:00Added an answer on June 13, 2026 at 9:22 am

    Yes you can reuse the Statement object, but the ResultSet objects returned by the executeQuery closes already opened resultsets.

    See the javadoc for the explanation

    By default, only one ResultSet object per Statement object can be open
    at the same time. Therefore, if the reading of one ResultSet object is
    interleaved with the reading of another, each must have been generated
    by different Statement objects. All execution methods in the Statement
    interface implicitly close a statment’s current ResultSet object if an
    open one exists.

    So the following occurs:

    // load driver
    Connection con = DriverManager.getConnection(..);
    Statement stmt = con.createStatement();
    ResultSet result = stmt.executeQuery("select ..");
    // do something with result ... or not
    ResultSet result2 = stmt.executeQuery("select ...");
    // result is now closed, you cannot read from it anymore
    // do something with result2
    stmt.close(); // will close the resultset bound to it
    

    For example you can find an open source implementation of Statement in the jTDS project.
    In the Statement.executeQuery() method you can see a call to initialize() that closes all the resultsets already opened

    protected void initialize() throws SQLException {
        updateCount = -1;
        resultQueue.clear();
        genKeyResultSet = null;
        tds.clearResponseQueue();
        // FIXME Should old exceptions found now be thrown instead of lost?
        messages.exceptions = null;
        messages.clearWarnings();
        closeAllResultSets();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

connecting to a mysql database from java using jdbc. declaring a query String query
I am fetching records from MySQL database using Java (JDBC). I have tables -
When I create a prepared statement like this in java (using JDBC): pStmt =
I'm accessing public mySQL database using JDBC and mySQL java connector . exonCount is
I would be calling procedure or function from java using JDBC . In terms
I have a java-application using JDBC for database interaction. I want to do a
I have a Java web app running on Tomcat 7 that is using JDBC
I'm using Mysql database and neatbeans IDE conncting by mysql-connector-java-5.1.21 jdbc , and I
I am using jdbc to connect to a postgresql database in a java application
I have connected Java program to Oracle database using JDBC. I want to store

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.