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.