Path: csiph.com!news.swapon.de!newsreader4.netcologne.de!news.netcologne.de!.POSTED.xdsl-89-1-156-35.netcologne.de!not-for-mail From: Patrick Roemer Newsgroups: comp.lang.java.programmer Subject: Re: Is this the way to work with try with resources Date: Tue, 8 Aug 2017 19:30:01 +0200 Organization: news.netcologne.de Distribution: world Message-ID: References: <87poc6wb9n.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Tue, 8 Aug 2017 17:30:01 +0000 (UTC) Injection-Info: newsreader4.netcologne.de; posting-host="xdsl-89-1-156-35.netcologne.de:89.1.156.35"; logging-data="25456"; mail-complaints-to="abuse@netcologne.de" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.24) Gecko/20100411 Thunderbird/2.0.0.24 Mnenhy/0.7.6.0 In-Reply-To: Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:37087 Responding to Stefan Ram: > Cecil Westerhof writes: >>It feels a bit long-winded. > > Yes. The method is too long. We need to refactor. Up to here we agree. ;) I'd rather move towards something like this: private static void statistics(Connection conn) { try (Statement stmt = conn.createStatement()) { String format = runQueryFun(stmt, COUNT_TOTAL_PROV, rs -> totalStats(rs.getInt("no"))); runQueryProc(stmt, COUNT_UNUSED_PROV, rs -> unusedStats(format, rs.getInt("no"))); runQueryProc(stmt, COUNT_USED_PROV, rs -> usedStats(format, rs.getInt("no"))); } catch (SQLException e) { System.err.printf("Error in statistics\n"); e.printStackTrace(); fatalError(""); // wat?! } } - runQuery[Fun|Proc] and associated interfaces could go to a reusable toolbox - all JDBC jazz is confined to this method; all methods called from here are JDBC agnostic Best regards, Patrick