Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I solve this "cannot be cast to" exception?

I am getting the following exception:

java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingStatement cannot be cast to org.apache.tomcat.dbcp.dbcp.DelegatingStatement
    optimise.database.ProdigyDB.doInsertSerial(ProdigyDB.java:139)
    optimise.stock.CurrentRequest.writeRequest(CurrentRequest.java:145)
    optimise.stock.SubmitOrderServlet.processRequest(SubmitOrderServlet.java:51)
    optimise.stock.SubmitOrderServlet.doPost(SubmitOrderServlet.java:188)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    optimise.stock.AuthFilter.doFilter(AuthFilter.java:69)

The line of code is this:

 serial = ((com.informix.jdbc.IfmxStatement) ((org.apache.tomcat.dbcp.dbcp.DelegatingStatement) statement).getDelegate()).getSerial();

The full function is this:

public static int doInsertSerial(String sql) {
    int serial = 0;

    try {
        Connection connection = getConnection();
        java.sql.Statement statement = connection.createStatement();
        statement.executeUpdate(sql);

        serial = ((com.informix.jdbc.IfmxStatement)((org.apache.tomcat.dbcp.dbcp.DelegatingStatement) statement).getDelegate()).getSerial();

        statement.close();
        connection.close();
    } catch (SQLException e) {
        throw new RuntimeException("SQL Update Exception\n" + sql, e);                
    }

    return serial;
}

This is mysterious because the same code works fine when deployed on a Unix SCO5 server but errors on a Linux Red Hat server.

Any help is much appreciated.

Thanks.


Looking into things a bit deeper, I see that the DelegatingStatement class shows an error:

public class DelegatingStatement extends AbandonedTrace implements Statement {

org.apache.commons.dbcp.DelegatingStatement is not abstract and does not override abstract method isPoolable() in java.sql.Statement

I do not understand why this is happening.

like image 310
Charlie Grant Avatar asked Oct 20 '22 07:10

Charlie Grant


1 Answers

java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingStatement cannot be cast to org.apache.tomcat.dbcp.dbcp.DelegatingStatement

The package is different for the DelegatingStatement class. org.apache.commons.dbcp & org.apache.tomcat.dbcp.dbcp are two different packages providing two different DelegatingStatement classes. You're getting a ClassCastException because the JVM doesn't see them as the same type at all.

Check the DelegatingStatement class package which is imported. As per the code org.apache.tomcat.dbcp.dbcp package should be imported.

Also check this & this

like image 155
underdog Avatar answered Oct 27 '22 09:10

underdog