2

I have this class

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

I want to use WSCallHelper.jdbcCall as suggested in WAS 8 info center but I won't able to get it correct. I tried

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

I'm getting NOT_A_JDBC_OBJECT error. When I didn't use the WSCallHelper, I'm getting

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

I also tried to use

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

(where call variable has SQL statement). It says that stmt is not wrapped.

I'm using the above class to connect to Oracle 9i database. Anyone know how to use WSCallHelper.jdbcCall for the code above? Thanks.

Archie
  • 23
  • 1
  • 5

3 Answers3

1

You can obtain an oracle connection using the following code.

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

where conn is a java.sql connection obtained through WAS data source. Now you can use oracleConn object to achieve the desired result.

bilalhaider
  • 181
  • 2
  • 5
  • 15
1
if (con!=null){
    con = (Connection) WSCallHelper.getNativeConnection((WSJdbcConnection) con);
}

This worked for me. get the Native connection from the com.ibm.websphere.rsadapter.WSCallHelper and cast it to oracle connection object.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Karthik
  • 21
  • 3
0

It should be pointed out there is also a JDBC spec standard solution to this problem:

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

Or for the connection,

OracleConnection oracleConn = conn.unwrap(OracleConnection.class);
OracleCallableStatement ocs = oracleConn.prepareCall(sql);
njr
  • 3,399
  • 9
  • 7