Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37096

Re: Is this the way to work with try with resources

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Cecil Westerhof <Cecil@decebal.nl>
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> (permalink)
References <87poc6wb9n.fsf@munus.decebal.nl> <refactor-20170808133822@ram.dialup.fu-berlin.de> <refactor-20170808141521@ram.dialup.fu-berlin.de>
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

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-08 13:26 +0200
  Re: Is this the way to work with try with resources Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-08 12:51 +0000
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:10 +0200
  Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-08 18:29 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:20 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:16 +0200
  Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-08 19:30 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:49 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:01 +0200
    Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-09 15:39 +0200
  Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:25 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:36 -0400
      Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:29 +0200
        Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 20:53 -0400
          Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 23:03 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:48 -0400
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:24 +0200
  Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:26 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 08:30 +0200
    Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 09:40 +0000
      Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 12:56 +0200
        Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 16:51 +0000
          Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 19:37 +0200
            Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 19:51 +0000
              Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 22:20 +0200
                Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 23:17 +0200
          Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-09 22:08 +0200
        Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:07 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:05 -0400
  Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:39 +0200

csiph-web