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:01:16 +0200 Organization: Decebal Computing Lines: 48 Message-ID: <8760dxwd0j.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="U2FsdGVkX1/wG2V82V1g8JC9ubQ9Nno+Yufbpd53Y08=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:siuscySfV6UNeiTswOky1vn/hx8= sha1:73rY8+3JFtM2ge0mLfCGaEBNbko= Xref: csiph.com comp.lang.java.programmer:37098 ram@zedat.fu-berlin.de (Stefan Ram) writes: > Patrick Roemer writes: >>I'd rather move towards something like this: >>String format = runQueryFun(stmt, COUNT_TOTAL_PROV, >> rs -> totalStats(rs.getInt("no"))); >>runQueryProc(stmt, COUNT_UNUSED_PROV, >> rs -> unusedStats(format, rs.getInt("no"))); > ... >>- 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 > > Excellent idea! I always like to separate parts of a library > according to the system (like JDBC) they deal with. I think > that this is also an important aspect of MVC, where the VC > contains GUI stuff, while the M contains non-GUI stuff. > > I think you might mean something along the lines of (untested): > > private static void > runQueryProc > ( final java.sql.Statement statement, > final java.lang.String query, > final java.util.function.Consumer< java.sql.ResultSet >consumer ) > throws java.sql.SQLException > { try( final java.sql.ResultSet set = statement.executeQuery( query )) > { consumer.accept( set ); }} > > private static< T >T > runQueryFun > ( final java.sql.Statement statement, > final java.lang.String query, > final java.util.function.Function< java.sql.ResultSet, T > function ) > throws java.sql.SQLException > { T result = null; > try( final java.sql.ResultSet set = statement.executeQuery( query )) > { result = function.apply( set ); } > return result; } Looks interesting. For the moment I found a solution. But this is certainly something to look at. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof