PreparedStatement rounds doubles to scale 14 during update

From: "Peter Kovacs" <maxottovonstirlitz(at)gmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: PreparedStatement rounds doubles to scale 14 during update
Date: 2007-09-05 09:27:19
Message-ID: b6e8f2e80709050227r420d7a3cx751f819fba658854@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi,

I have the following problem with postgresql-8.1-407.jdbc3.jar used
against a 8.1 version backend.

A table "chemterms_cols_test" has a column "logp" defined as:

logp numeric(30,17)

The double value 6.118992224252588 gets rounded to 6.11899222425259000
during updates with PreparedStatements using place-holder. (There is
no rounding with Statement and the value specified in the SQL
literally.) Is this the expected behaviour?

Below is the code that can be used to test this behaviour ("cd_id" is
the primary key):

public static void main(String[] args) {
try {
Connection conn = DBTools.getConnectionHandler().getConnection();
conn.setAutoCommit(false);

int cdId = 1;
double dblValue = 6.118992224252588;

String sql =
"update chemterms_cols_test set logp = ? where cd_id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
try {
pstmt.setDouble(1, dblValue);
pstmt.setInt(2, 1);
pstmt.executeUpdate();
conn.commit();
} finally {
pstmt.close();
}

ResultSet rs = null;
sql = "select logp from chemterms_cols_test where cd_id = ?";
pstmt = conn.prepareStatement(sql);
try {
pstmt.setInt(1, 1);
rs = pstmt.executeQuery();
rs.next();
double actual = rs.getDouble(1);
System.out.println("actual=" + actual + ", expected="
+ dblValue + ", equals? : " + (actual == dblValue));

} finally {
try {
if (rs != null) {
rs.close();
}
} finally {
pstmt.close();
}
}
} finally {
conn.close();
}
} catch (Throwable tbl) {
tbl.printStackTrace();
System.exit(1);
}
}

Thanks
Peter

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Heikki Linnakangas 2007-09-05 10:01:30 Re: PreparedStatement rounds doubles to scale 14 during update
Previous Message Heikki Linnakangas 2007-09-05 08:35:06 Re: [JDBC] Questión of JDBC