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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:43:57+00:00 2026-05-18T01:43:57+00:00

I have JDBC connection code similiar to the following from the Java JDBC tutorial:

  • 0

I have JDBC connection code similiar to the following from the Java JDBC tutorial:

public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from " + dbName + ".COFFEES";
    try {
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery(query);
      while (rs.next()) {
        String coffeeName = rs.getString("COF_NAME");
        int supplierID = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");
        int sales = rs.getInt("SALES");
        int total = rs.getInt("TOTAL");
        System.out.println(coffeeName + "\t" + supplierID + "\t" + price + "\t" + sales + "\t" + total);
      }
    } catch (SQLException e ) {
      JDBCTutorialUtilities.printSQLException(e);
    } finally {
      stmt.close();
    }
  }

My problem with this way of handling to connection is that it closes the statement in the finally block and the method throws any SQLException that may occur. I don’t want to do that, because I want any problems handled within this class. However, I do want that Statement#close() call in the finally block so that it is always closed.

Right now I’m placing this code in a separate method that returns a HashMap of the fields returned to that the exception is handled in-class. Is there another, possibly better way to handle this?

EDIT: The close() SQLException is the one I am concerned with. If possible I’d like to handle that within the method. I could write a try/catch in the finally, but that seems really awkward.

  • 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-05-18T01:43:58+00:00Added an answer on May 18, 2026 at 1:43 am

    You have several problems:

    • You need to close the ResultSet explicitly. Some drivers are less forgiving about forgetting to close the ResultSet than others, it doesn’t hurt anything to be sure to close it.

    • You ought to catch the SQLException thrown by the Statement.close, because it’s not interesting and only serves to mask the interesting exception (if you have something in this method throw an exception, then the finally throws an exception on the way out, you get the exception from the finally block and lose the first exception). There really isn’t anything that you can do if a close method call throws an exception, just log it and go on, it is not something to be concerned about.

    • You should give up on the idea of handling all sqlexceptions in this method, the SQLException thrown by statement.executeQuery is the one that is worthwhile and it ought to be propagated if something goes wrong. It’s likely other code in your application will want to know if your sql here succeeded or not and that’s what throwing exceptions is for.

    Personally I’d suggest using a library like Ibatis or spring-jdbc for this. JDBC is error-prone and tedious and it’s better to take advantage of existing tools.

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

Sidebar

Related Questions

I have the following code: @Test public void springTest() throws SQLException{ //Connect to the
Suppose I have the following code DataSource source = (DataSource) (new InitialContext()).lookup(jdbc/myName); Connection connnection
Lets say i have the following bit of code import java.sql.Connection; import java.sql.DriverManager; import
currently i have jdbc code with the following basic stucture: get Connection (do the
I have the following situation... I use CallableStatement from java.sql package. When I use
I have this code: try { Class.forName(net.sourceforge.jtds.jdbc.Driver); Connection conn = DriverManager.getConnection( jdbc:jtds:sqlserver://myMachine:1433/myDB;instance=sql2008;user=myUserName;password=myPassword; ); System.out.println(connected);
I need to test a JDBC connection to a database. The java code to
I have a Java JDBC application that uses multiple threads to retrieve information from
I have the following method being called in a Java EE web application. public
I have the following code that's trying to establish a connection to a remote

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.