2

我有这门课

package com.middleware.jdbc.j2ee.websphere;
public class WS50NativeJDBCExtractorImpl implements NativeJDBCExtractorInf {
private Class webSphere5ConnectionClass;        
private Method webSphere5NativeConnectionMethod;    
private Class webSphere5StatementClass;   
private Method webSphere5NativeStatementMethod;

public WS50NativeJDBCExtractorImpl() throws Exception {
    try {
        this.webSphere5ConnectionClass = getClass().getClassLoader().loadClass("com.ibm.ws.rsadapter.jdbc.WSJdbcConnection");
        Class jdbcAdapterUtilClass = getClass().getClassLoader().loadClass("com.ibm.ws.rsadapter.jdbc.WSJdbcUtil");
        this.webSphere5NativeConnectionMethod = jdbcAdapterUtilClass.getMethod("getNativeConnection", new Class[] { this.webSphere5ConnectionClass });
        this.webSphere5StatementClass = getClass().getClassLoader().loadClass("com.ibm.ws.rsadapter.jdbc.WSJdbcStatement");
        this.webSphere5NativeStatementMethod = webSphere5StatementClass.getDeclaredMethod("getJDBCImplObject", null);
        this.webSphere5NativeStatementMethod.setAccessible(true);
    } catch (Exception ex) {
        e.printStackTrace();
    }
}
public Connection getConnection(Connection c) throws Exception {
    if (this.webSphere5ConnectionClass != null && this.webSphere5ConnectionClass.isAssignableFrom(c.getClass())) {
        try {
            return (Connection) this.webSphere5NativeConnectionMethod.invoke(null, new Object[] { c });
        } catch (Throwable e) {
            e.printStackTrace();
        }
    } else {
        return c;
    }
}
private Object primGetStatement(Statement stmt) throws Exception {
    if (this.webSphere5StatementClass != null && this.webSphere5StatementClass.isAssignableFrom(stmt.getClass())) {
        try {
            return this.webSphere5NativeStatementMethod.invoke(stmt, null);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    } else {
        return stmt;
    }
}
public CallableStatement getCallableStatement(CallableStatement cs) throws Exception {
    return (CallableStatement) primGetStatement(cs);
}}

我想按照 WAS 8 信息中心的建议使用 WSCallHelper.jdbcCall,但我无法正确使用它。我试过了

Object st = WSCallHelper.jdbcCall(null, this.webSphere5StatementClass, "getJDBCImplObject", new Object[]{}, new Class[]{});

我收到 NOT_A_JDBC_OBJECT 错误。当我没有使用 WSCallHelper 时,我得到了

java.lang.ClassCastException: oracle.jdbc.driver.OracleCallableStatementWrapper incompatible with oracle.jdbc.OracleCallableStatement

我也尝试使用

CallableStatement cStmt = getCallableStatement(call);
if(stmt.isWrapperFor(OracleCallableStatement.class)){
    OracleCallableStatement ocs = (OracleCallableStatement) stmt;
}

(其中调用变量具有 SQL 语句)。它说 stmt 没有包装。

我正在使用上面的类连接到 Oracle 9i 数据库。任何人都知道如何在上面的代码中使用 WSCallHelper.jdbcCall 吗?谢谢。

4

3 回答 3

1

您可以使用以下代码获取 oracle 连接。

OracleConnection oracleConn=null;
if (conn!=null){
oracleConn= (OracleConnection) WSJdbcUtil.getNativeConnection((WSJdbcConnection) conn);
}

其中 conn 是通过 WAS 数据源获得的 java.sql 连接。现在您可以使用 oracleConn 对象来实现所需的结果。

于 2012-10-15T09:59:35.277 回答
1
if (con!=null){
    con = (Connection) WSCallHelper.getNativeConnection((WSJdbcConnection) con);
}

这对我有用。从 中获取本机连接com.ibm.websphere.rsadapter.WSCallHelper并将其转换为 oracle 连接对象。

于 2016-06-05T00:07:36.127 回答
0

需要指出的是,对于这个问题还有一个 JDBC 规范标准的解决方案:

OracleCallableStatement ocs = stmt.unwrap(OracleCallableStatement.class);

或者为了连接,

OracleConnection oracleConn = conn.unwrap(OracleConnection.class);
OracleCallableStatement ocs = oracleConn.prepareCall(sql);
于 2020-10-21T13:47:37.163 回答