Using PreparedStatement.setObject fails for some types with ClassCastException when using Oracle JDBC Driver
 Technote (troubleshooting)
 
Problem(Abstract)
The following uses of setObject fail with ClassCastException instead of successfully updating the table. These uses are incorrect.

// Test use the following tables

create table Bit_Tab (MAX_VAL SMALLINT, MIN_VAL SMALLINT, NULL_VAL SMALLINT NULL);

create table Decimal_Tab (MAX_VAL DECIMAL(30,15),MIN_VAL DECIMAL(30,15), NULL_VAL DECIMAL(30,15) NULL);

create table Bigint_Tab (MAX_VAL NUMBER(19,0), MIN_VAL NUMBER(19,0), NULL_VAL NUMBER(19,0) NULL);

create table Timestamp_Tab (IN_TIME DATE, NULL_VAL DATE NULL);

// Tests use the following prepared statements
pstmt1i = con.prepareStatement("insert into Bit_Tab VALUES(?, ?, ?)");
pstmt2i = con.prepareStatement("insert into Decimal_Tab VALUES(?, ?, ?)");
pstmt3i = con.prepareStatement("insert into Bigint_Tab VALUES(?, ?, ?)");
pstmt4i = con.prepareStatement("insert into Timestamp_Tab VALUES(?, ?)");
pstmt5i = con.prepareStatement("insert into Decimal_Tab VALUES(?, ?, ?)");

// Failing code snippets
// Test 1 - Boolean
pstmt1i.setBoolean(1,true);
pstmt1i.setBoolean(2,true);
java.math.BigDecimal decimalVal = new java.math.BigDecimal(1);
Boolean minDecimalVal=new Boolean(decimalVal.toString());
pstmt1i.setObject(3,minDecimalVal,java.sql.Types.DECIMAL,2);
pstmt1i.executeUpdate();

// Test 2 - Float
pstmt2i.setFloat(1,2.1F);
pstmt2i.setFloat(2,2.2F);
String stringVal = "1.1";
Float floatVal = new Float(stringVal);
pstmt2i.setObject(3,floatVal,java.sql.Types.DECIMAL,15);
pstmt2i.executeUpdate();

// Test 3 - Long
pstmt3i.setFloat(1,3.1F);
pstmt3i.setFloat(2,3.2F);
Long val = new Long(1);;
String stringValLing = new String(val.toString());
Long longVal = new Long(stringValLing);
pstmt3i.setObject(3,longVal,java.sql.Types.CHAR);
pstmt3i.executeUpdate();

// Test 4 - Timestamp
pstmt4i.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt4i.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
Timestamp getTSval = new Timestamp(System.currentTimeMillis());
String stringValTs = getTSval.toString();
Timestamp timeStampVal = Timestamp.valueOf(stringValTs);
pstmt4i.setObject(2,timeStampVal,java.sql.Types.DATE);
pstmt4i.executeUpdate();

// Test 5 - BigDecimal
pstmt5i.setFloat(1, 5.1F);
pstmt5i.setFloat(2, 5.2F);
String stringValBd = "1.1";
stringValBd = stringValBd.trim();
java.math.BigDecimal bigDecimalVal = new BigDecimal(stringValBd);
pstmt5i.setObject(3,bigDecimalVal,java.sql.Types.CHAR);
pstmt5i.executeUpdate();
 
Cause
This problem is addressed by Oracle Bug 2640294.
 
Resolving the problem
Avoid using setObject method. Use the setXXX method for the corresponding data type XXX (for example, setFloat for data type Float).
Note: This information is also included in WebSphere Application Server 5.0.1 Release Notes.
 
 
Cross Reference information
Segment Product Component Platform Version Edition
Application Servers Runtimes for Java Technology Java SDK
 
 


Document Information


Product categories: Software > Application Servers > Distributed Application & Web Servers > WebSphere Application Server > Java 2 Connectivity (J2C)
Operating system(s): Windows
Software version: 5.0.2.6
Software edition:
Reference #: 1114363
IBM Group: Software Group
Modified date: Jun 9, 2004