title: How do I solve this "cannot be cast to" exception?
body: <p>I am getting the following exception:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>The line of code is this:</p>
  
  <pre><code> serial = ((com.informix.jdbc.IfmxStatement) ((org.apache.tomcat.dbcp.dbcp.DelegatingStatement) statement).getDelegate()).getSerial();
  </code></pre>
  
  <p>The full function is this:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>This is mysterious because the same code works fine when deployed on a Unix SCO5 server but errors on a Linux Red Hat server.</p>
  
  <p>Any help is much appreciated.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Looking into things a bit deeper, I see that the DelegatingStatement class shows an error:</p>
  
  <p>public class DelegatingStatement extends AbandonedTrace implements Statement {</p>
  
  <p>org.apache.commons.dbcp.DelegatingStatement is not abstract and does not override abstract method isPoolable() in java.sql.Statement</p>
  
  <p>I do not understand why this is happening.</p>