java.lang.ClassCastException:oracle.sql.TIMESTAMP无法转换为java.sql.Timestamp [英] java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp

查看:1399
本文介绍了java.lang.ClassCastException:oracle.sql.TIMESTAMP无法转换为java.sql.Timestamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发通过网络流ResultSet的应用程序.我最终使用了CachedRowSetImpl类.但是,当我连接到Oracle数据库时,会出现这样的错误

I am working on an application that streams ResultSet over a network. I ended up using a CachedRowSetImpl class. But when I connect to an Oracle DB, I get an error like this

java.lang.ClassCastException:oracle.sql.TIMESTAMP无法转换为java.sql.Timestamp

java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp

请帮助.

源代码如下:

ResultSet res = response.getResultSet(); //resultset from the server
while (res.next()) {
    Agent agent = new Agent();
    agent.setName(res.getString(2));
    agent.setMobile(res.getString(1));
    agent.setBalance(res.getLong(4));
    agent.setLastUpdate(res.getDate(3)); //date from the result set
    agent.setAccountNumber(res.getString(5));
}

错误...

java.lang.ClassCastException:oracle.sql.TIMESTAMP无法转换为java.sql.Timestamp java.lang.ClassCastException:oracle.sql.TIMESTAMP无法转换为java.sql.Timestamp 在com.sun.rowset.CachedRowSetImpl.getDate(CachedRowSetImpl.java:2139)

java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp at com.sun.rowset.CachedRowSetImpl.getDate(CachedRowSetImpl.java:2139)

推荐答案

The javadoc for ResultSet.getObject() mandates that the JDBC type should be mapped to a Java type as prescribed by the JDBC spec (TIMESTAMP -> java.sqlTimestmp):

此方法将以Java形式返回给定列的值 目的. Java对象的类型将是默认的Java对象 对应于列的SQL类型的类型,遵循以下映射 JDBC规范中指定的内置类型.

This method will return the value of the given column as a Java object. The type of the Java object will be the default Java object type corresponding to the column's SQL type, following the mapping for built-in types specified in the JDBC specification.

您已经注意到,Oracle驱动程序默认情况下不符合标准,而是使用oracle.sql.TIMESTAMP(不扩展java.sql.Timestamp).好消息是,您可以通过在虚拟机启动期间将 oracle.jdbc.J2EE13Compliant 系统属性设置为true来强制JDBC遵从:

As you have noticed, the Oracle driver is by default not compliant with the standard and uses oracle.sql.TIMESTAMP instead (which does not extend java.sql.Timestamp). The good news is that you can force JDBC compliance by setting the oracle.jdbc.J2EE13Compliant system property to true during vm startup:

java -Doracle.jdbc.J2EE13Compliant=true YourApplication

或以编程方式

System.getProperties().setProperty("oracle.jdbc.J2EE13Compliant", "true")

执行此操作后,getResult()将按预期返回java.sql.Timestamp的实例.

Once you do this, getResult() will return instances of java.sql.Timestamp, as expected.

有关更多详细信息,请参见 Oracle JDBC驱动程序文档中的相关部分

For more details see the relevant section from the Oracle JDBC Driver Documentation, which describes several ways of setting oracle.jdbc.J2EE13Compliant.

这篇关于java.lang.ClassCastException:oracle.sql.TIMESTAMP无法转换为java.sql.Timestamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆