Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Is this the way to work with try with resources Date: Wed, 09 Aug 2017 06:26:55 +0200 Organization: Decebal Computing Lines: 82 Message-ID: <87lgmtwels.fsf@munus.decebal.nl> References: <87poc6wb9n.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="75662a87a83d79e99d1b79ff65a19ab5"; logging-data="8354"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XaozSmMq8TJ1/MeNSzUV7uYtC4qwk5Qo=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:zrEk/F/NGHV3z/Z7zOwC9Ks+NSA= sha1:04nF4s59TuGxB0YRAHvElg4HbzA= Xref: csiph.com comp.lang.java.programmer:37095 Cecil Westerhof writes: > I thought that I could rewrite it to: > private static void statistics() { > try (Statement stmt = conn.createStatement(); > ResultSet rsTotal = stmt.executeQuery(COUNT_TOTAL_PROV); > ResultSet rsUnused = stmt.executeQuery(COUNT_UNUSED_PROV); > ResultSet rsUsed = stmt.executeQuery(COUNT_USED_PROV)) { > int count; > String format; > > count = rsTotal.getInt("no"); > format = String.format("There are %%,%dd %%6s proverbs.%%n", String.format("%,d", count).length()); > System.out.printf(format, count, "total"); > count = rsUnused.getInt("no"); > if (count != 0) { > System.out.printf(format, count, "unused"); > } > count = rsUsed.getInt("no"); > System.out.printf(format, count, "used"); > } catch (SQLException e) { > System.err.printf("Error in statistics\n"); > e.printStackTrace(); > fatalError(""); > } > } > > That would be a little better I think, but when there are a lot of > queries to be done, you need to define a lot of extra result-sets. > > But there is a problem with that. If I do that, all three result-sets > have the same query: the last one. > What is the reason for this? I am using SQLite3. I do not know enough about the JDBC. When looking at: https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html It clearly states that you normally only can have one current result-set pro statement. It says there: All execution methods in the Statement interface implicitly close a current ResultSet object of the statement if an open one exists. But that is clearly not true, because the other ones still work, but are replaced with the other one. The solution is to have a statement pro result-set, but than this statement is only necessary for getting the result-set. So I rewrote statistics to: private static void statistics() { try (ResultSet rsTotal = executeQuery(COUNT_TOTAL_PROV); ResultSet rsUnused = executeQuery(COUNT_UNUSED_PROV); ResultSet rsUsed = executeQuery(COUNT_USED_PROV)) { int count; String format; count = rsTotal.getInt("no"); format = String.format("There are %%,%dd %%-6s proverbs.%%n", String.format("%,d", count).length()); System.out.printf(format, count, "total"); count = rsUnused.getInt("no"); if (count != 0) { System.out.printf(format, count, "unused"); } count = rsUsed.getInt("no"); System.out.printf(format, count, "used"); } catch (SQLException e) { System.err.printf("Error in statistics\n"); e.printStackTrace(); fatalError(""); } } And wrote the function executeQuery to make the code a little more compact: private static ResultSet executeQuery(String query) throws SQLException { return conn.createStatement().executeQuery(query); } -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof