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?
views:
92answers:
4
+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
2010-08-17 16:06:54
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
2010-08-17 16:11:31
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
2010-08-17 16:34:05
@Derek Mahar: Sorry, I don't have any ORM experience
OMG Ponies
2010-08-17 16:49:59
+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
2010-08-17 16:13:22
That's what I thought, too, but my cast to `BigDecimal` throws a `ClassCastException`.
Derek Mahar
2010-08-17 16:16:38
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
2010-08-17 16:20:54
+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
2010-08-17 16:21:37