views:

92

answers:

4

What Java data type does the Oracle JDBC driver assign to the Oracle SQL data type NUMERIC? Does this vary with the size of the NUMERIC type?

+1  A: 

It's been a while, but I believe it's BIGINT in Java.It's BigDecimal. I remember the classcastexception you'd encounter would give a hint...

OMG Ponies
You'd think the `ClassCastException` would help, but in this case, we use Hibernate to execute the query and the method returns `Object`.
Derek Mahar
At http://stackoverflow.com/questions/3504751/what-object-type-does-spring-hibernate-template-execute-method-return-for-a-count, I ask a question more specific to our particular case which uses Spring Hibernate Template.
Derek Mahar
@Derek Mahar: Sorry, I don't have any ORM experience
OMG Ponies
+2  A: 

According to the Oracle documentation it is java.math.BigDecimal.


"but my cast to BigDecimal throws a ClassCastException"

Have you tried using oracle.sql.NUMBER ?

APC
That's what I thought, too, but my cast to `BigDecimal` throws a `ClassCastException`.
Derek Mahar
According to that table, though the Oracle data type is always `NUMERIC`, the JDBC data type depends on the underlying SQL data type which for `INTEGER` or `SMALLINT`, would be `int`.
Derek Mahar
@APC: I'll inspect the type to find out.
Derek Mahar
+5  A: 

As others have already said: the driver maps everything to BigDecimal, even if it's defined as NUMBER(38) (which could be mapped to BigInteger)

But it's pretty easy to find out what the driver maps. Simply do a getObject() on the column of the ResultSet and see which class the driver generated.

Something like:

ResultSet rs = statement.executeQuery("select the_number_column from the_table");
if (rs.next())
{
  Object o = rs.getObject(1);
  System.out.println("Class: " + o.getClass().getName());
}
a_horse_with_no_name