Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I handle this exception within this method?
    primarykey
    data
    text
    <p>I have JDBC connection code similiar to the following from the Java JDBC tutorial:</p> <pre><code>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(); } } </code></pre> <p>My problem with this way of handling to connection is that it closes the statement in the <code>finally</code> 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 <em>do</em> want that <code>Statement#close()</code> call in the finally block so that it is always closed.</p> <p>Right now I'm placing this code in a separate method that returns a <code>HashMap</code> of the fields returned to that the exception is handled in-class. Is there another, possibly better way to handle this?</p> <p>EDIT: The <code>close()</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload