Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37096
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Is this the way to work with try with resources |
| Date | 2017-08-09 06:39 +0200 |
| Organization | Decebal Computing |
| 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> |
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
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