db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel John Debrunner <...@apache.org>
Subject Re: setObject(idx, bigDecimal, Types.NUMERIC); doesn't work ?
Date Fri, 06 Jan 2006 17:34:50 GMT
Lance J. Andersen wrote:

> The spec needs to be followed whether you agree or disagree with the
> semantics.  The javadocs are very specific here and there must have been
> a reason for this decision at the time.  As i indicated to Craig, I am
> trying to find out if any of the previous spec leads recall why this was
> done before i do anything else on this issue.

I think the spec is correct.

The reason that the scale defaults to zero is that it's the only choice.
There is no guarantee that the scale can be obtained from the passed in
object. I.e. what happens if the object is not a BigDecimal, what would
the scale be?

Here is a summary of the ways that a DECIMAL column could be set from an
Object.

setBigDecimal(int, BigDecimal) - DECIMAL value passed as-is to engine

setString(int, String) -  VARCHAR or LONGVARCHAR value passed as-is to
engine

setObject(int, Object) - same as setBigDecimal if value is an instance
of BigDecimal, or same as setString is value is an instance of String.
Note type mapping (Java to SQL) is defined by the class of the value.

setObject(int, Object, int) - DECIMAL value passed to engine has scale
set to zero if type is Type.DECIMAL or Type.NUMERIC
Note type mapping (Java to SQL) is defined by the type (3rd) parameter.

setObject(int, Object, int, int) - DECIMAL value passed to engine has
passed in scale if type is Type.DECIMAL or Type.NUMERIC
Note type mapping (Java to SQL) is defined by the type (3rd) parameter.

Note the big difference is that the last two setObject methods define
the type of the value sent to the engine from the type parameter, not
from the class of the Object. Thus, in these methods, any object to SQL
type conversion has to happen in the jdbc driver, not the engine.

E.g. for setting on a DECIMAL column

// Type passed to engine as a VARCHAR
// converted to DECIMAL by *database engine*.
ps.setObject(1, "2.3");

// Type passed to engine as a DECIMAL
// converted to DECIMAL(n,0) by *JDBC driver*.
ps.setObject(1, "2.3", Types.DECIMAL);

// Type passed to engine as a DECIMAL
// converted to DECIMAL(n,3) by *JDBC driver*.
ps.setObject(1, "2.3", Types.DECIMAL, 3);


Of course, with Derby embedded the line between database engine and JDBC
driver does not exist, but that's the abnormal case. The typical JDBC
driver is a client driver.
Dan.



Mime
View raw message