Thanks, you resolved my problem!
If it can help someone, this is how to do:
public static void main( String[] args ) {
try {
StringBuffer sb = new StringBuffer();
Properties props = new Properties();
// Connexion
props.setProperty( "user", "postgres" );
Connection _connexion = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/test", props );
// Create and fill the table
sb.append( "create temporary table test_oliv (val float);"
);
sb.append( "insert into test_oliv(val) values (1.8);" );
sb.append( "insert into test_oliv(val) values (1.8);" );
sb.append( "insert into test_oliv(val) values (1.8);" );
_connexion.prepareStatement( sb.toString() ).execute();
// Request
ResultSet rs = _connexion.prepareStatement( "select
sum(val) as t from test_oliv;" ).executeQuery();
rs.next();
System.out.println( "Before:" );
// Responses from jdbc driver: not the same
displayed by the basic psql editor of postgres.
System.out.println( "Value: " + rs.getFloat( "t" ) );
rs.close();
_connexion.prepareStatement(
"SET extra_float_digits=0" ).execute();
rs = _connexion.prepareStatement( "select sum(val) as t
from test_oliv;" ).executeQuery();
rs.next();
System.out.println( "After:" );
// Responses from jdbc driver: the same displayed
by the basic psql editor of postgres.
System.out.println( "Value: " + rs.getFloat( "t" ) );
rs.close();
_connexion.close();
} catch ( Exception e ) {
e.printStackTrace();
}
}
Regards
Olivier Bouiron
Le 28/04/2010 02:04, Oliver Jowett a écrit :
Olivier
Bouiron wrote:
Under the psql editor, when I want the sum of
these 3 rows, the answer is 5.4.
But, under Java using the JDBC connector, the answer is 5.399999
I tried to use an other connector (postgresql-8.2-510.jdbc4) and the
problem disapears.
I'd guess that it is due to newer drivers setting extra_float_digits.
See http://archives.postgresql.org/pgsql-jdbc/2009-05/msg00077.php
-O
--
Olivier Bouiron
Institut Sainte Catherine
1750, chemin du lavarin
84000 AVIGNON
Tel: 04.90.27.57.43