0

I was reading the following official guide and I've found a problem, using this two snippets of code together will lead into an error (no scope for the stmt object):

Processing ResultSet Objects

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);
    }
}

Closing Connections

} finally {
    if (stmt != null) { stmt.close(); }
}

If I try to stmt.close() in the finally block I'll get an error because there's not a stmt variable in his scope, and that's because (as far as I've understand) the actual scope of the stmt object is in the try block.

My question is simple, does this two blocks of code works together?

The answer I've found is no, only moving the instantiation of the stmt object outside the try block will lead in a working snippet.

Can someone please give me his thought?
I just want to understand if there are some aspects of the argument that aren't still clear to me.

Thanks a lot to anyone will try to help me.

Shanrya
  • 5
  • 1
  • 2
    In the tutorial `stmt` is declared before the `try` block, so it should be in scope for the `finally`. Are you sure you didn't actually declare it in the first line of the `try` block? – Federico klez Culloca Jan 15 '20 at 12:05
  • Need the full code, i.e. where is stmt declared (as Federico said above). If it is declared outside of the try then this should compile. – Thomas Cook Jan 15 '20 at 12:07
  • You two are right, the stmt is declared before the try-catch block, with a null value assigned. My apologies, I was overthinking about this and I probably missed the essential point. Anyway, thanks to both of you for the help! – Shanrya Jan 15 '20 at 14:35

2 Answers2

0

Presumably, you have declared the stmt object inside the try and not outside it. The code should look like this:

Statement stmt;
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);
    }
} finally {
    if (stmt != null) { stmt.close(); }
}

However, you can use the fact that Statement implements AutoClosable and actually omit the finally block by using try with resources (a Java feature added in Java 6 iirc). That would look like:

try (Statement 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) {
     // Log it
}

You can read more about AutoClosable and try with resources here:

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Thomas Cook
  • 4,371
  • 2
  • 25
  • 42
0

to make this explicit, you should assign stmt to null, so it exists and is initialzed:

Statement stmt = null;
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);
    }
} finally {
    if (stmt != null) { stmt.close(); }
}
Axel Podehl
  • 4,034
  • 29
  • 41
  • You're right, and the tutorial which I was reading is exactly like your example, the problem is that i missed that point. Thanks for your precious help! – Shanrya Jan 15 '20 at 14:36