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:39:42 +0200 Organization: Decebal Computing Lines: 69 Message-ID: <87efslwe0h.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="10296"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Gk7nv3qGom66H9msVvsAZpfoF7eUmqCU=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:sj+/pb58Af3e6+2ShvbPGEnVekM= sha1:igqYhX8qSeZK0/iTjmSZOrWLCH0= Xref: csiph.com comp.lang.java.programmer:37096 ram@zedat.fu-berlin.de (Stefan Ram) writes: > ram@zedat.fu-berlin.de (Stefan Ram) writes: >>private static void printUnused >>( final java.sql.Statement statement, >> final java.lang.String format, >> final int count ) > > For those who think that now there are too many > parameters, here's a parameterless version: > > private static java.sql.Statement statement; > private static int count; > private static java.lang.String format; > > private static void printTotal() > { System.out.printf( format, count, "total" ); } > > private static void printUnused() > throws java.sql.SQLException > { try( final java.sql.ResultSet rsUnused = statement.executeQuery( COUNT_UNUSED_PROV )) > { final int countUnused = rsUnused.getInt( "no" ); > if( countUnused != 0 )java.lang.System.out.printf( format, count, "unused" ); }} > > private static void printUsed() > throws java.sql.SQLException > { try( final java.sql.ResultSet rsUsed = statement.executeQuery( COUNT_USED_PROV )) > { final int countUsed = rsUsed.getInt( "no" ); > java.lang.System.out.printf( format, countUsed, "used" ); }} > > private static void printThreeStatistics() > throws java.sql.SQLException > { printTotal(); > printUnused(); > printUsed(); } > > private static void makeFormat() > throws java.sql.SQLException > { final java.lang.String format = java.lang.String.format > ( "There are %%,%dd %%6s proverbs.%%n", java.lang.String.format( "%,d", count ).length() ); > printThreeStatistics(); } > > private static void makeCount() > throws java.sql.SQLException > { try( final java.sql.ResultSet rsTotal = statement.executeQuery( COUNT_TOTAL_PROV )) > { count = rsTotal.getInt( "no" ); > makeFormat(); }} > > private static void makeStatement() > { try( final java.sql.Statement s = conn.createStatement() ) > { statement = s; makeCount(); } > catch( final java.sql.SQLException exception ) > { java.lang.System.err.println( "Error in statistics" ); > exception.printStackTrace(); > fatalError( "" ); }} > > private static void printStatistics() > { makeStatement(); } To be honest I find that also a bit long-winded. I found a way to do it with one try statement. See my reply on my own post. The problem was that a Statement can only have one ResultSet. I should study the JDBC api. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof