Discussions

General J2EE: Apache DBCP ClassCastException : DelegatingCallableStatement

  1. I am getting a ClassCastException : org.apache.commoms.dbcp.DelegationgCallableStatement

    while using DBCP1.2.1. I am using commons-dbcp1.2.1 with TomCat 4.1.29. to get the DBCPConnectionPool Object.

    I use the OracleCallableStatement Object to call the geCursor() to retrieve the Cursor

    Please see the Code snippet which generates error

    Connection conn=null;
    CallableStatement stmt=null;
    ResultSet rs=null;

    try{
       //datasource will contain the javax.sql.DataSource after lookup
       //Here we get the org.apache.commons.dbcp.PoolableConnection Object
       conn=dataSource.getConnection();
       stmt=conn.prepareCall(sql);
       stmt.registerOutParameter(1,OracleTypes.CURSOR);
       ....//set the statement object values here
       stmt.execute();
       
        //get the Cursor here, here we get the ClassCastException
       rs=((OracleCallableStatement)stmt).getCursor(1);


    As an alternative, I tried to use the DelegatingConnection.getInnremostDelegate() to get the underlying connection and create the statement object from that. Still I get the same exception.

    As this is an existing code, i have to use the OracleCallableStatement to get the Cursor.

    Can you please suggest a work around for this ASAP

    Thanks
    Eldo Itteera
  2. rs=((OracleCallableStatement)stmt).getCursor(1);

    Can you please suggest a work around for this ASAP

    Try

    rs = (ResultSet) stmt.getObject(1);

    instead.

    Good luck,
    Stefan
  3. Thanks for your suggestion.Thats worked out.

    but I have another issue. In another part of the code, I need to use Oracle BLOB to update the Object output stream.
    Previously I have written the code to get the BLOB as follows. The statement object i used is like this

    Connection conn=ds.getConnection();

    Statement stmt=conn.createStatement(sql);

    ResultSet rs=stmt.executeQuery();

    //Here it will give the Class Cast exception for the DelegatingResultSet
    BLOB lob_data=((OracleResultSet)rs).getBLOB().

    ObjectOutputStream oos=new ObjectOutputStream(lob_data.getBinaryOutputStream());
    oos.writeObject(content);

    If I use Blob instead of Oracle BLOB, we dont have a method geBinaryOutputStream() in Blob, which is mandatory for me to update BLOB


    Since this is available only in OracleResultSet, it wont work even if I take the ordinary result set.

    Can you please suggest a work around for this

    Thanks in advance
    Eldo
  4. ObjectOutputStream oos=new ObjectOutputStream(lob_data.getBinaryOutputStream());
    oos.writeObject(content);

    If I use Blob instead of Oracle BLOB, we dont have a method getBinaryOutputStream() in Blob, which is mandatory for me to update BLOB.

    Why not simply

    oracle.sql.BLOB lob_data = (oracle.sql.BLOB) rs.getBlob(i); ?


    Kind regards,
    Stefan
  5. Thanks for your suggestion

    Eventhough syntactically its is correct and compile the source code successfully, At runtime thsi gives ClassCastException oracle.sql.BLOB

    Is there any other workaround for this?

    Regards
    Eldo
  6. If I use Blob instead of Oracle BLOB, we dont have a method getBinaryOutputStream() in Blob, which is mandatory for me to update BLOBS

    Try java.sql.Blob#setBinaryStream(long pos) instead of oracle.sql.BLOB#getBinaryOutputStream(long pos). This is new in JDBC 3.0 (JDK 1.4) and should work with the Oracle 10g JDBC driver.

    Kind regards,
    Stefan