ClassCastException With 11.2.0.2 Oracle JDBC Driver When Getting XMLTYPE Column Value (Doc ID 1305209.1)

Last updated on AUGUST 03, 2016

Applies to:

JDBC - Version 11.2.0.2.0 to 11.2.0.2.0 [Release 11.2]
Information in this document applies to any platform.
***Checked for relevance on 21-Jan-2014***

Symptoms

When getting XMLTYPE column value as an oracle.xdb.XMLType object,
with the version 11.2.0.2 of  Oracle JDBC driver, the following exception is thrown:

java.lang.ClassCastException: oracle.jdbc.driver.OracleSQLXML cannot be cast to oracle.xdb.XMLType



To illustrate this problem, the warehouse_spec XMLTYPE column is defined and filled as the following:

CREATE TABLE warehouses(
warehouse_id NUMBER(4),
warehouse_spec XMLTYPE,
warehouse_name VARCHAR2(35),
location_id NUMBER(4));

INSERT INTO warehouses VALUES (
100,
XMLType(
'<Warehouse whNo="100">
<Building>Owned</Building>
</Warehouse>'),
'Tower Records',
1003);
/



In order to fetch the  XMLTYPE column value, the following code is executed:

import oracle.xdb.XMLType;
..
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select warehouse_spec from warehouses");
Object o_xml = rs.getObject(1);
XMLType xmltype = (XMLType)o_xml; // here : with 11.2.0.2 driver ClassCastException
System.out.println(" xmltype value = " + xmltype.getStringVal());



The problem (ClassCastException is thrown)  is happening with 11.2.0.2 Oracle JDBC driver:

java.lang.ClassCastException: oracle.jdbc.driver.OracleSQLXML cannot be cast to
oracle.xdb.XMLType
at TestMLType.test(TestMLType.java:88)
at TestMLType.main(TestMLType.java:135)



Executing this code against the 11.2.0.1 JDBC driver, the XMTYPE column value can be obtained:

java TestMLType
DriverVersion: [11.2.0.1.0]
DriverMajorVersion: [11]
DriverMinorVersion: [2]
DriverName: [Oracle JDBC driver]
URL: [jdbc:oracle:thin:@localhost:1521:tools6]
UserName: [SCOTT]
Oracle
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
The JDK version is 1.6.0_22
xmltype value = <Warehouse whNo="100">
<Building>Owned</Building>
</Warehouse>



You can download the sample code (TestMLType.java) from here.
To execute this TestMLType program, just ensure:



Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms