I use Spring’s JdbcTemplate to run an insert SQL statement. The field I want to insert into is a NUMBER. The value is: -0.11111111 of type float. However, after insertion into DB, the value I get is padded with random numbers -0.1111111119389534.
Note that when I use direct JDBC, the value is inserted as it is, without the padded numbers.
I use BeanPropertySqlParameterSource and MapSqlParameterSource for setting the parameters of the INSERT statement, both giving same results. The code looks like this:
BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(stat);
int n = jt.update(query, params);
where jt is an instant of SimpleJdbcTemplate.
The DB is Oracle.
Thank you.
For Java’s
floatdatatype, Spring JDBC behaves differently than direct JDBC.Consider SQL statement
INSERT INTO table (field) VALUES (-0.11111111)withfieldOracle typeNUMBERand -0.11111111 of type float.With direct JDBC, it gives the value of
fieldas it is, i.e. -0.11111111.But with Spring JDBC (using
JdbcTemplate.update(), it gives the value offieldpadded with numbers, i.e. -0.1111111119389534.No such difference occurs for Java type
double. The inserted value is not padded with numbers.With Java datatype
BigDecimal, the inserted value into DB will also be padded with numbers, i.e. -0.1111111099999999990428634077943570446223 and this is consistent for both direct JDBC and Spring JDBC.The results for
floatis understandable, as Java states in its documentation that “(Float) data type should never be used for precise values, such as currency.” So eventhough the behaviour is different in direct JDBC and Spring JDBC, we can’t say it’s wrong.For
BigDecimal, read http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal%28double%29