Dan
Dan

Reputation: 11089

SQL DECIMAL to Java Number

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?

Upvotes: 7

Views: 21430

Answers (2)

FallAndLearn
FallAndLearn

Reputation: 4135

You can get decimal value from the table and convert it into Double as:

BigDecimal res = ResultSet.getBigDecimal();

Upvotes: 4

Misa Lazovic
Misa Lazovic

Reputation: 2823

The recommended Java mapping for SQL DECIMAL and NUMERIC values is BigDecimal:

ResultSet.getBigDecimal()

Upvotes: 10

Related Questions