简体   繁体   中英

ClassCastException: java.lang.Double incompatible with java.math.BigDecimal

This exception is coming in very strange way.

I am reading a variable's value from a stored procedure whose type is DECIMAL(15,4). And at java end I was type casting this value to BigDecimal , and it was working perfectly fine. Now I started getting this exception : java.lang.ClassCastException: java.lang.Double incompatible with java.math.BigDecimal and to resolve this I am casting this variable type to Double and then casting it back to BigDecimal using

New :

Double myVarDb= //reading some variable's value with type-> DECIMAL(15,4) from Db end
 BigDecimal myVar = (BigDecimal)BigDecimal.valueOf(myVarDb);

Old :

 BigDecimal  myVar= //reading some variable's value with type-> DECIMAL(15,4) 

If in case anyone has faced this issue or has any idea why it might come, please guide.

UPDATE : I checked both drivers and below are the versions :

Old : DriverVersion: 3.6.60

NEW : DriverVersion: 3.64.106

The instances which I am getting created after reading the values are of type:

Old : BigDecimal

NEW : Double

So now the problem is clear that why are we getting this exception because we are getting Double object now as default object.

So can anyone throw some light on why these different types of instances are getting created? Is this because of different versions of JDBC driver?

Why would you get the value as Double then try to cast it to bigDecimal, which is wrong. There's no need to make it harder and more complex.

In Java and JDBC all DECIMAL values are mapped with BigDecimal and there's the getBigDecimel() method of ResultSet which allows you to directly get it asa BigDecimal.

You will simply use this line of code:

BigDecimal myVar = resultSet.getBigDecimal(0);

Instead of all of this (which is very bad):

Double myVarDb= resultSet.getDouble(0);
BigDecimal myVar = (BigDecimal)BigDecimal.valueOf(myVarDb);

You can see from Mapping SQL and Java Types that:

The recommended Java mapping for the DECIMAL and NUMERIC types is java.math.BigDecimal, a Java type that also expresses fixed-point numbers with absolute precision. The java.math.BigDecimal type provides math operations to allow BigDecimal types to be added, subtracted, multiplied, and divided with other BigDecimal types, with integer types, and with floating point types.

The method recommended for retrieving DECIMAL and NUMERIC values is ResultSet.getBigDecimal. JDBC also allows access to these SQL types as simple Strings or arrays of char. Thus, Java programmers can use getString to receive a DECIMAL or NUMERIC result. However, this makes the common case where DECIMAL or NUMERIC are used for currency values rather awkward, since it means that application writers have to perform math on strings. It is also possible to retrieve these SQL types as any of the Java numeric types.

Note:

In your old code the reason why you were getting a cast Exception may be because you were not using the .getBigDecimal() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM