Path: csiph.com!news.swapon.de!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 07:10:09 +0200 Organization: Decebal Computing Lines: 72 Message-ID: <871solwclq.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="14029"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Vk4uyvtHE4aFbnEMHHRzHnU5xWeGKFs4=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:LtII1FsV3fN8rdht6S2NMKByeX8= sha1:lwCoZtGFIROaKkD/SBE5dU2GQMM= Xref: csiph.com comp.lang.java.programmer:37099 Martin Gregorie writes: > On Tue, 08 Aug 2017 13:26:44 +0200, Cecil Westerhof wrote: > >> 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(""); >> } >> } >> > In the absence of any other information I prefer the second organisation. I have done that now. I found the reason the above went wrong: a Statement can only have one concurrent ResultSet. See my reply on my own post. >> 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. >> > Thats difficult to answer knowing how ACID compliant SQLite3 is and I need to read the JDBC specs. When creating a new ResultSet the old one(s) get overwritten with the new one. (Not closed as the specs say.) > commitment unit: reuse of "no", presumably a column name, in all > three I probably should rename that to count. ;-) > statements bothers me a bit and so does the fact that you've executed all > three queries before asking for any results. That is the only way to have only one try with resources. > As I result of both these > factors I think you've ended up retrieving the result from the LAST query > three times. Yes, I need to have a separate Statement for each ResultSet. That is the way I implemented it now. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof