oracle.jdbc.OracleCallableStatement cast exception

2019-09-06 21:54发布

问题:

I have a problem about casting a CallableStatement to OracleCallableStatement. It gives ClassCastException like this:

java.lang.ClassCastException:
    oracle.jdbc.driver.OracleCallableStatementWrapper cannot be cast to
    oracle.jdbc.driver.OracleCallableStatement

And the code is:

Connection conn = qdbDataSource.getConnection();
PreparedStatement pstmt = null;
Connection conn2 = ((WLConnection)conn).getVendorConnection();
try {
    CallableStatement cs = conn2.prepareCall("{ ?=call asr.bsc(?,?,?,?,?,?,?)}");
    OracleCallableStatement ocs = (OracleCallableStatement)cs;
// (...)
}

I tried to use spring jdbc template, but result was the same.

I am using WebLogic 10.3.2 and the driver class of the datasource is default one. I'm also using the ojdbc14.jar in my project, the startup classpath does not include it.

Any ideas?

EDIT: These are the subclasses of the runtime wrapper class:

weblogic.jdbc.wrapper.CallableStatement_oracle_jdbc_driver_OracleCallableStatementWrapper class
weblogic.jdbc.wrapper.CallableStatement class
weblogic.jdbc.wrapper.PreparedStatement class
weblogic.jdbc.wrapper.Statement class
weblogic.jdbc.wrapper.JDBCWrapperImpl class
weblogic.utils.wrapper.WrapperImpl class java.lang.Object

回答1:

Cast result of getVendorConnection() to OracleConnection, than use OracleCallableStatement instead of CallableStatement,

oracle.jdbc.OracleConnection conn2 = (oracle.jdbc.OracleConnection)(((WLConnection)conn).getVendorConnection());


回答2:

java.lang.ClassCastException: oracle.jdbc.driver.OracleCallableStatementWrapper cannot be cast to oracle.jdbc.driver.OracleCallableStatement

So i work few weeks ago with OracleCallableStatement and i solved it with import ojdbc6.jar.

So you just add to your project this file and it offers directly OracleCallableStatement,

OraclePreparedStatement etc..

All what you need is import oracle.jdbc.OracleCallableStatement; and it will works.

Connection con = null;
OracleCallableStatement cs = null;
try {
   con = OracleDAOFactory.getOracleDatabaseConnection();
   cs = (OracleCallableStatement) con.prepareCall(SOME_PROCEDURE);
   ...
}

Have look at this.



回答3:

I found it. It was the ojdbc jar under my lib folder. I am using a statement in weblogic.xml like:

prefer-webinf-classes

And this provides to use the jar files under web-inf/lib in the first place. So when it finds an ojdbc.jar under that folder, it just fits for my application but not for weblogic itself. Because weblogic has its own ojdbc jar under it and somehow, it just extends the OracleCallableStatement class to Wrap them from its own ojdbc jar. Because I have a separate ojdbc jar, at runtime, it could not cast it to my jar's OracleCallableStatement. When I removed the jar under web-inf/lib and gave the responsibility about jdbc connections and statements parts to weblogic, it worked.

Thanks fellas



回答4:

I will give best solution, one approach is using <wls:prefer-web-inf-classes>false</ wls:prefer-web-inf-classes > in weblogic.xml. If you want true instead of false in web-inf-classes then another approach is remove ojdbcXX.jar in lib folder. While compiling program you can use any ojdbcXX.jar in classpath. But while deploying remove ojdbcXX.jar from lib. So that weblogic application server will use its own ojdbc and it will run successfully without any error.