views:

212

answers:

1

Have a table and a sproc setup in Sybase as follows:

create table testtab (f float)

create proc insert_testtab @f float as insert testtab values(@f)

And a java object that holds a Double

class TestObj { Double getF() { return 12.34; } }

Using SimpleJdbcCall & BeanPropertySqlParameterSource:

SqlParameterSource params = new BeanPropertySqlParameterSource(new TestObj());
SimpleJdbcCall call = new SimpleJdbcCall(dataSource).withProcedureName("insert_testtab");
call.execute(params);

What happens is that 12.0 gets inserted into the database, rather than 12.34. It appears that underneath the covers, BeanPropertySqlParameterSource as passing the number to the sproc as a java.sql.Types.NUMERIC and is truncating the decimals plases.

Can anyone help explain this, is this a problem maybe with the Sybase code in Spring, or maybe I am doing something incorrectly?

+1  A: 

Your best best is to register the type of the property with the BeanPropertySqlParamterSource object. Otherwise, Spring uses setObject on the underlying CallableStatement. This leaves it all up to the JDBC driver implementation to decide how the data gets treated. Something like params.registerSqlType("f", java.sql.Types.DECIMAL) ought to do the trick. You will also need to declare params as an instance of BeanPropertySqlParameterSource for this to work.

laz
Thanks for that, I will try it, isn't the BeanPropertySqlParameterSource provided so you don't have to supply parameter names and types? This seems to be a bug to me.
I thought so too, but I am working with a JDBC driver that doesn't handle calls to setObject nicely, causing problems similar to what you are seeing. You have to see how the particular JDBC driver you are using treats a Double to know if it is a bug for sure.
laz