Question

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?

Was it helpful?

Solution

long does not support decimal places because it is an integer type. Have you tried queryForObject(sql, BigDecimal.class, params)?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top