Peter Walser
Peter Walser

Reputation: 15706

Oracle JDBC: underflow in double

When trying to insert doubles into a DOUBLE PRECISION column of an Oracle table, I get an exception when the double is out of range (in my case: too small), but only when using PreparedStatement (if I use the normal Statement, it rounds the double to 0).

Table:

create table test
(
  VALUE DOUBLE PRECISION
);

Java code:

double d = 1e-234;

// using Statement works, the number gets rounded and 0 is inserted
try (Statement stmt = conn.createStatement()) {
    stmt.executeUpdate("insert into test values (" + d + ")");
}

// using PreparedStatement fails, throws an IllegalArgumentException: Underflow
try (PreparedStatement stmt = conn.prepareStatement("insert into test values (?)")) {
    stmt.setDouble(1, d);
    stmt.executeUpdate();
}

Thanks for any insights/hints.

Upvotes: 2

Views: 2183

Answers (2)

Gregor Sturm
Gregor Sturm

Reputation: 2910

DOUBLE PRECISION = FLOAT(126) as you noted in the comment.

Use the BINARY_DOUBLE data type to have the same precision as a Java Double.

Reference:

Upvotes: 3

atokpas
atokpas

Reputation: 3351

That is expected because the double precision will be converted to number ultimately and number only supports range-1E-130 .. 10E125.

SQL> select to_char(to_number(1.0e-234)) from dual;

T
-
0

SQL> select to_char(to_number(1e-130)) from dual;

TO_CHAR(TO_NUMBER(1E-130))
----------------------------------------
1.000000000000000000000000000000000E-130

FLOAT:

A subtype of the NUMBER datatype having precision p. A FLOAT value is represented internally as NUMBER. The precision p can range from 1 to 126 binary digits. A FLOAT value requires from 1 to 22 bytes. More...

From AskTom

Doesn't show that floats aren't numbers, they are just a number with a different precision then number in itself.

Upvotes: 1

Related Questions