Spring queryForLong - Loss of precision
I have a Spring application running against a MySql database. I have a sales table like:
+-------------------+------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------------------+------+-----+---------+-------+
| Price | decimal(19,4) | NO | | NULL | |
| ItemId | int(10) unsigned | NO | | NULL | |
+-------------------+------------------------------+-----开发者_如何学编程-+-----+---------+-------+
In my Spring class, I have the following:
long price = getSimpleJdbcTemplate().queryForLong("SELECT price FROM sales WHERE itemID = :itemID", params);
logger.info("price: " + price + ");
However, this is returning a price of 1, even if this query directly on the database returns 0.8500. How do I avoid this loss of precision from the decimal in the database to the long in my code?
long
does not support decimal places because it is an integer type. Have you tried queryForObject(sql, BigDecimal.class, params)
?
精彩评论