java 我如何解决这个“无法转换为”异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31379629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I solve this "cannot be cast to" exception?
提问by Charlie Grant
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.
这很神秘,因为相同的代码在 Unix SCO5 服务器上部署时可以正常工作,但在 Linux Red Hat 服务器上会出错。
Any help is much appreciated.
任何帮助深表感谢。
Thanks.
谢谢。
Looking into things a bit deeper, I see that the DelegatingStatement class shows an error:
深入研究一下,我发现 DelegatingStatement 类显示了一个错误:
public class DelegatingStatement extends AbandonedTrace implements Statement {
公共类 DelegatingStatement 扩展 AbandonedTrace 实现 Statement {
org.apache.commons.dbcp.DelegatingStatement is not abstract and does not override abstract method isPoolable() in java.sql.Statement
org.apache.commons.dbcp.DelegatingStatement 不是抽象的并且不会覆盖 java.sql.Statement 中的抽象方法 isPoolable()
I do not understand why this is happening.
我不明白为什么会这样。
回答by underdog
java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingStatement cannot be cast to org.apache.tomcat.dbcp.dbcp.DelegatingStatement
java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingStatement 不能转换为 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.
DelegatingStatement 类的包是不同的。org.apache.commons.dbcp
&org.apache.tomcat.dbcp.dbcp
是两个不同的包,提供两个不同的 DelegatingStatement 类。您收到 ClassCastException 是因为 JVM 根本不将它们视为相同的类型。
Check the DelegatingStatement class package which is imported. As per the code
org.apache.tomcat.dbcp.dbcp
package should be imported.
检查导入的 DelegatingStatement 类包。根据代码
org.apache.tomcat.dbcp.dbcp
包应该被导入。