简体   繁体   中英

Call Oracle Stored Procedure from Java

Here is my Java code for calling the stored procedure. I keep getting an error saying: java.lang.ClassCastException: oracle.jdbc.driver.OracleCallableStatementWrapper incompatible with oracle.jdbc.OracleCallableStatement

 public Connection initiateDBConnection() throws NamingException, SQLException { Connection result = null; InitialContext initialContext = new InitialContext(); DataSource datasource = (DataSource) initialContext .doLookup(Constants.DATASOURCE_CONTEXT); result = (Connection) WSJdbcUtil .getNativeConnection((WSJdbcConnection) datasource .getConnection()); return result; } public void callStoredProcedure(String procedureName, Map<Integer, Object> map) throws SQLException, NamingException { OracleCallableStatement statement = (OracleCallableStatement) initiateDBConnection() .prepareCall(procedureName); Iterator<Entry<Integer, Object>> params = map.entrySet().iterator(); while (params.hasNext()) { Entry<Integer, Object> contents = params.next(); statement.setNString(contents.getKey(), (String) contents.getValue()); System.out.println("Key: " + contents.getKey() + "Value: " + contents.getValue()); } statement.execute(); statement.close(); } 

It appears the prepareCall method return OracleCallableStatementWrapper object.

OracleCallableStatementWrapper statement = (OracleCallableStatementWrapper) initiateDBConnection().prepareCall(procedureName);

Also,You could try with JAVA JDBC API as follows:

CallableStatement statement = (CallableStatement) initiateDBConnection()
            .prepareCall(procedureName);

I have noticed in your cose WSJdbcConnection which means you are using Websphere. If this is indeed the case, and if you really need to use Oracle specific clasess ( OracleConnection , etc.) I would suggest that you should look into the WSCallHelper class, provided by websphere for 'clean' management of this kind of situation. It provides two main methods, jdbcCall and jdbcPass for calling methods on and passing native types respectively.

More information is available in this article from IBM: http://www-01.ibm.com/support/docview.wss?uid=swg21409335

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