从Java调用Oracle存储过程

时间:2011-12-24 08:52:15

标签: java stored-procedures oracle11g

这是我用于调用存储过程的Java代码。我一直收到错误说: java.lang.ClassCastException:oracle.jdbc.driver.OracleCallableStatementWrapper与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();
}

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(); }

2 个答案:

答案 0 :(得分:0)

prepareCall方法似乎返回OracleCallableStatementWrapper对象。

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

此外,您可以尝试使用JAVA JDBC API,如下所示:

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

答案 1 :(得分:0)

我在你的cose WSJdbcConnection中注意到了这意味着你正在使用Websphere。确实如此,如果你确实需要使用Oracle特定的clasess(OracleConnection等),我建议您查看由websphere提供的WSCallHelper类'clean'管理这种情况。它提供了两个主要方法jdbcCalljdbcPass,分别用于调用方法和传递本机类型。

IBM的这篇文章提供了更多信息:http://www-01.ibm.com/support/docview.wss?uid=swg21409335