java.lang.ClassCastException: oracle.sql.TIMESTAMP 不能转换为 java.sql.Timestamp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13269564/
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
java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp
提问by Adeniran Opeyemi
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
我正在开发一个通过网络流式传输 ResultSet 的应用程序。我最终使用了 CachedRowSetImpl 类。但是当我连接到 Oracle DB 时,出现这样的错误
java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp
java.lang.ClassCastException: oracle.sql.TIMESTAMP 不能转换为 java.sql.Timestamp
Please help.
请帮忙。
The source code is as follows:
源代码如下:
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));
}
The error ...
错误 ...
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)
java.lang.ClassCastException:oracle.sql.TIMESTAMP 无法转换为 java.sql.Timestamp java.lang.ClassCastException:oracle.sql.TIMESTAMP 无法转换为 java.sql.Timestamp at com.sun.rowset.CachedRowSetImpl.getDate (CachedRowSetImpl.java:2139)
回答by Bogdan Calmac
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):
ResultSet.getObject()的 javadoc要求 JDBC 类型应该映射到 JDBC 规范(TIMESTAMP -> java.sqlTimestmp)规定的 Java 类型:
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.
此方法将给定列的值作为 Java 对象返回。Java 对象的类型将是对应于列的 SQL 类型的默认 Java 对象类型,遵循 JDBC 规范中指定的内置类型映射。
As you have noticed, the Oracle driver is by default not compliant with the standardand 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.J2EE13Compliantsystem property to true
during vm startup:
正如您所注意到的,Oracle 驱动程序默认不符合标准,oracle.sql.TIMESTAMP
而是使用(不扩展java.sql.Timestamp
)。好消息是,您可以通过在 vm 启动期间将oracle.jdbc.J2EE13Compliant系统属性设置为强制 JDBC 合规性true
:
java -Doracle.jdbc.J2EE13Compliant=true YourApplication
or programmatically
或以编程方式
System.getProperties().setProperty("oracle.jdbc.J2EE13Compliant", "true")
Once you do this, getResult() will return instances of java.sql.Timestamp
, as expected.
执行此操作后, getResult() 将按java.sql.Timestamp
预期返回 的实例。
For more details see the relevant section from the Oracle JDBC Driver Documentation, which describes several ways of setting oracle.jdbc.J2EE13Compliant.
有关更多详细信息,请参阅Oracle JDBC 驱动程序文档中的相关部分,其中描述了几种设置 oracle.jdbc.J2EE13Compliant 的方法。
回答by Adeniran Opeyemi
I found a way out.
我找到了出路。
oracle.sql.TIMESTAMP ts = (oracle.sql.TIMESTAMP) res.getObject("last_update");
agent.setLastUpdate(new Date(ts.dateValue().getTime()));
回答by Amir Amiri
Add this this line to the JVM setting. It will work.
将此行添加到 JVM 设置中。它会起作用。
-Doracle.jdbc.J2EE13Compliant=true
回答by Vincent Malgrat
This is because oracle.sql.TIMESTAMP
is not derived from java.sql.TIMESTAMP
:
这是因为oracle.sql.TIMESTAMP
不是源自java.sql.TIMESTAMP
:
java.lang.Object -> oracle.sql.Datum -> oracle.sql.TIMESTAMP
java.lang.Object -> oracle.sql.Datum -> oracle.sql.TIMESTAMP
You can't cast the former into the later.
您不能将前者转换为后者。
Instead use oracle.sql.TIMESTAMP.timestampValue()
:
而是使用oracle.sql.TIMESTAMP.timestampValue()
:
public Timestamp timestampValue(Calendar cal) throws SQLException
Calls
toTimestamp
to convert internal OracleDate
and Calendar to a JavaTimestamp
.
public Timestamp timestampValue(Calendar cal) throws SQLException
调用
toTimestamp
将内部 OracleDate
和 Calendar 转换为 JavaTimestamp
。
回答by Sam ツ
This can be solved by using the timestampValue() function present in the oracle.sql.TIMESTAMP class. This method will convert oracle.sql.TIMESTAMP into java.sql.Timestamp.
这可以通过使用 oracle.sql.TIMESTAMP 类中的 timestampValue() 函数来解决。该方法会将 oracle.sql.TIMESTAMP 转换为 java.sql.Timestamp。
oracle.sql.TIMESTAMP ts = (oracle.sql.TIMESTAMP) res.getObject("last_update"); agent.setLastUpdate(ts.timestampValue());
oracle.sql.TIMESTAMP ts = (oracle.sql.TIMESTAMP) res.getObject("last_update"); agent.setLastUpdate(ts.timestampValue());
回答by Mark Rotteveel
Most likely you used getTIMESTAMP()
instead of getTimestamp()
. The methods getTIMESTAMP()
(and getDATE()
are extension in OracleResultSet
which return Oracle specific types.
很可能你使用了getTIMESTAMP()
而不是getTimestamp()
. 方法getTIMESTAMP()
(并且getDATE()
是OracleResultSet
返回 Oracle 特定类型的扩展。
If not then you are not using a JDBC driver, because the returntype of getDate()
is java.sql.Date
and of getTimestamp()
is java.sql.Timestamp
, so it can't be a totally different type as in your question.
如果不是,那么您没有使用 JDBC 驱动程序,因为getDate()
isjava.sql.Date
和 of getTimestamp()
is的返回类型java.sql.Timestamp
,所以它不能是与您的问题完全不同的类型。
回答by Abubakkar
I think the problem is your setLastUpdate
is expecting an object of java.sql.Date
type.
我认为问题在于您setLastUpdate
正在期待一个java.sql.Date
类型的对象。
Now when you use agent.setLastUpdate(res.getDate(3));
现在当你使用 agent.setLastUpdate(res.getDate(3));
The res.getDate(3)
must be returning an object that your method doesn't expect so your there may be ClassCastException
for that.
在res.getDate(3)
必须返回一个对象,你的方法并不期望这样你有可能ClassCastException
为。
Try this code and see whether it solves your problem or not:
试试这个代码,看看它是否解决了你的问题:
agent.setLastUpdate(new java.util.Date(res.getDate(3).getTime()));