I have a database (MySQL 5.6 if it matters) in which I would like to store numeric values that will usually, but not always, be integers. Right now I have a DECIMAL(10, 3)
column which seems to be working well. What I'm looking for is a method I know doesn't exist in native JDBC, but would be something like:
public Number ResultSet.getNumber()
which would return me a Long
(or maybe even Integer
) if the underlying SQL value is actually an integer, and a Double
otherwise. Is there some way to get an equivalent result from an SQL DECIMAL column in JDBC?
The recommended Java mapping for SQL DECIMAL and NUMERIC values is BigDecimal:
ResultSet.getBigDecimal()
You can get decimal value from the table and convert it into Double as:
BigDecimal res = ResultSet.getBigDecimal();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With