Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37100
| 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 07:20 +0200 |
| Organization | Decebal Computing |
| Message-ID | <87shh1uxjx.fsf@munus.decebal.nl> (permalink) |
| References | <87poc6wb9n.fsf@munus.decebal.nl> <omcou4$lkj$1@newsreader4.netcologne.de> <SQL-statements-20170808174606@ram.dialup.fu-berlin.de> |
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> Patrick Roemer <sangamon@netcologne.de> writes:
>>| By default, only one ResultSet object per Statement object can be open
>>| at the same time. Therefore, if the reading of one ResultSet object is
>
> Ok. As far as I understand it, the refactoring posted by me
> does not suffer from violations against this restriction.
>
> Still, I have revised my code so as to make the try regions
> for the statements as small as possible in order to make it
> readably clear that there is no such violation:
>
> (Note that especially »makeFormatAndContinue()« now is
> evaluated outside of the scope of »rsTotal«, so now there
> are no result sets that even have overlapping lifetimes.)
>
> 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
> { int countUnused = 0;
> try( final java.sql.ResultSet rsUnused = statement.executeQuery( COUNT_UNUSED_PROV ))
> { countUnused = rsUnused.getInt( "no" ); }
> if( countUnused != 0 )java.lang.System.out.printf( format, count, "unused" ); }
>
> private static void printUsed()
> throws java.sql.SQLException
> { int countUsed = -1;
> try( final java.sql.ResultSet rsUsed = statement.executeQuery( COUNT_USED_PROV ))
> { countUsed = rsUsed.getInt( "no" ); }
> java.lang.System.out.printf( format, countUsed, "used" ); }
>
> private static void printAllStatistics()
> throws java.sql.SQLException
> { printTotal();
> printUnused();
> printUsed(); }
>
> private static void makeFormatAndContinue()
> throws java.sql.SQLException
> { format = java.lang.String.format
> ( "There are %%,%dd %%6s proverbs.%%n", java.lang.String.format( "%,d", count ).length() );
> printAllStatistics(); }
>
> private static void makeCountAndContinue()
> throws java.sql.SQLException
> { try( final java.sql.ResultSet rsTotal = statement.executeQuery( COUNT_TOTAL_PROV ))
> { count = rsTotal.getInt( "no" ); }
> makeFormatAndContinue(); }
>
> private static void makeStatementAndContinue()
> { try( final java.sql.Statement s = conn.createStatement() )
> { statement = s; makeCountAndContinue(); }
> catch( final java.sql.SQLException exception )
> { java.lang.System.err.println( "Error in statistics" );
> exception.printStackTrace();
> fatalError( "" ); }}
>
> private static void printStatistics()
> { makeStatementAndContinue(); }
>
> public static void main( final java.lang.String[] args )
> { java.lang.System.out.println( "OK" ); }}
Looks interesting, but personally I find it to complex.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next 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