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: Is this the way to work with try with resources Date: Tue, 08 Aug 2017 13:26:44 +0200 Organization: Decebal Computing Lines: 71 Message-ID: <87poc6wb9n.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="fbcb25d28702fa7a6864db6c98c1fe4a"; logging-data="21781"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19CYiT6U8J86UMfpoX3xo5NdMUIKS+ucwU=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:er7lfKRx4f1c6NPXZvJB0kd4lQE= sha1:tAyNtNhsYN+Gy+VCI3Mh7JdbBLA= Xref: csiph.com comp.lang.java.programmer:37077 When using with resources you ‘should’ always work with try with resources. At the moment I have something like the following code: private static void statistics() { try (Statement stmt = conn.createStatement()) { String format; try (ResultSet rsTotal = stmt.executeQuery(COUNT_TOTAL_PROV)) { final int count = rsTotal.getInt("no"); format = String.format("There are %%,%dd %%6s proverbs.%%n", String.format("%,d", count).length()); System.out.printf(format, count, "total"); } try (ResultSet rsUnused = stmt.executeQuery(COUNT_UNUSED_PROV)) { final int count = rsUnused.getInt("no"); if (count != 0) { System.out.printf(format, count, "unused"); } } try (ResultSet rsUsed = stmt.executeQuery(COUNT_USED_PROV)) { final int count = rsUsed.getInt("no"); System.out.printf(format, count, "used"); } } catch (SQLException e) { System.err.printf("Error in statistics\n"); e.printStackTrace(); fatalError(""); } } It feels a bit long-winded. Is this the way to go, or is there a better way? 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. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof