PreparedStatement rounds doubles to scale 14 during update - Mailing list pgsql-jdbc

From Peter Kovacs
Subject PreparedStatement rounds doubles to scale 14 during update
Date
Msg-id b6e8f2e80709050227r420d7a3cx751f819fba658854@mail.gmail.com
Whole thread Raw
Responses Re: PreparedStatement rounds doubles to scale 14 during update
List 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

pgsql-jdbc by date:

Previous
From: "Heikki Linnakangas"
Date:
Subject: Re: [JDBC] Questión of JDBC
Next
From: "Heikki Linnakangas"
Date:
Subject: Re: PreparedStatement rounds doubles to scale 14 during update