Thread: scale parameter to setObject method

scale parameter to setObject method

From
Bob Bruynooghe
Date:
Hi,

I am calling the method setObject(int parameterIndex, Object x, int
targetSqlType) with NUMERIC as the targetSqlType and a BigDecimal
value of -92233720368.54775 as x. I expected the value placed in the
database to have no fractional part. In fact the database contains
-92233720368.54775. (Checked by both a JDBC query and using psql.)
The  prepared statement I am using is

INSERT INTO alltypes (key, short, int, long, bigdecimal, float,
double) ( ?, ?, ?, ?, ?, ?, ? )

and, the table was created using

CREATE TABLE alltypes  (key smallint PRIMARY KEY, short smallint, int
integer, long bigint, bigdecimal numeric, float real, double double
precision)

I have also tried using the method setObject(int parameterIndex,
Object x, int targetSqlType, int scale) and setting the scale
explicitly. The value of scale is being ignored.

I had a quick look at code for void setObject(int parameterIndex,
Object x, int targetSqlType, int scale) in AbstractJdbc2Statement in
the CVS repository. It looks to me like the scale parameter is not
used within the method. If I have understood the code correctly then I
would expect the behaviour I am seeing. However, I don't think that is
the behaviour that the spec defines. Am I understanding things
correctly?

The server is PostgreSQL 8.2.5 and the JDBC driver is
postgresql-8.3-603.jdbc3.

regards

Bob Bruynooghe

Re: scale parameter to setObject method

From
Kris Jurka
Date:

On Fri, 5 Sep 2008, Bob Bruynooghe wrote:

> I am calling the method setObject(int parameterIndex, Object x, int
> targetSqlType) with NUMERIC as the targetSqlType and a BigDecimal value of
> -92233720368.54775 as x. I expected the value placed in the database to have
> no fractional part. In fact the database contains -92233720368.54775.

I'm not sure why you expect no fraction here, the numeric type does
support fractional parts.

> I have also tried using the method setObject(int parameterIndex, Object
> x, int targetSqlType, int scale) and setting the scale explicitly. The
> value of scale is being ignored.

Yes, this is a bug in the driver.

Kris Jurka

Re: scale parameter to setObject method

From
Bob Bruynooghe
Date:
On 11 Sep 2008, at 18:36, Kris Jurka wrote:

>
>
> On Fri, 5 Sep 2008, Bob Bruynooghe wrote:
>
>> I am calling the method setObject(int parameterIndex, Object x, int
>> targetSqlType) with NUMERIC as the targetSqlType and a BigDecimal
>> value of -92233720368.54775 as x. I expected the value placed in
>> the database to have no fractional part. In fact the database
>> contains -92233720368.54775.
>
> I'm not sure why you expect no fraction here, the numeric type does
> support fractional parts.

Well, it is because the Javadoc for the method says the following:
     "This method is like the method setObject above, except that it
assumes a scale of zero."

The method that refers to is the version of setObject with 4
parameters and the Javadoc for that says
    "scale - for java.sql.Types.DECIMAL or java.sql.Types.NUMERIC
types, this is the number of digits after the decimal point."

The implementation of the 3 parameter setObject  method in
AbstractJdbc2Statement is

public void setObject(int parameterIndex, Object x, int targetSqlType)
throws SQLException {
         setObject(parameterIndex, x, targetSqlType, 0);
}
which matches the "assume a scale of zero".

This is the first time I have looked at this method and I was
surprised that the specified behaviour was to set the scale to zero
for NUMERIC parameters. I have no idea how other JDBC drivers
implement the method.
>
>
>> I have also tried using the method setObject(int parameterIndex,
>> Object x, int targetSqlType, int scale) and setting the scale
>> explicitly. The value of scale is being ignored.
>
> Yes, this is a bug in the driver.
>
> Kris Jurka


regards
Bob Bruynooghe