引起:java.lang.ClassCastException:java.sql.Timestamp无法强制转换为java.sql.Date [英] Caused by: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.sql.Date
问题描述
我收到以下代码片段的以下给定错误:
I am getting the below given error for the following code snippets:
try {
cRows = new CachedRowSetImpl();
while(cRows.next())
{
MyClass myClass = new MyClass();
myClass.setPrevDate(cRows.getDate("PREV_DATE")); // In debug mode, the error was throwing when I press Resume from here.
}
}
错误:
Caused by: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.sql.Date
在数据库中,该列的数据类型仅为 DATE
。我无法弄清楚时间戳
来自何处。
In the database, the datatype for the column is DATE
only. I am not able to figure out where the Timestamp
is coming here.
推荐答案
<我已经对这个问题进行了研究,并找到了一些有用的链接。我发现DATE和TIMESTAMP之间的这种混淆是特定于JDBC驱动程序的。大多数链接建议使用 -Doracle.jdbc.V8Compatible = true
。对于我的JBoss,我在 run.bat
中设置了此问题,问题得到了解决。
I have done a research on this issue and found some useful links. I found this confusion between DATE and TIMESTAMP is JDBC Driver specific. And most of the links suggest the use of -Doracle.jdbc.V8Compatible=true
. For my JBoss I have set this in run.bat
and the issue got resolved.
http://www.coderanch.com/t/90891/JBoss/oracle-jdbc-Compatible-true
oracle doc分享不同的解决方案:
The oracle doc shares different solutions:
-
更改表以使用TIMESTAMP而不是DATE。这很可能是
,但它是最好的解决方案。
Alter your tables to use TIMESTAMP instead of DATE. This is probably rarely possible, but it is the best solution when it is.
改变你的应用程序使用defineColumnType来定义列
作为TIMESTAMP而不是DATE。这有问题因为
你真的不想使用defineColumnType,除非你必须(参见
什么是defineColumnType以及我何时应该使用它?)。
Alter your application to use defineColumnType to define the columns as TIMESTAMP rather than DATE. There are problems with this because you really don't want to use defineColumnType unless you have to (see What is defineColumnType and when should I use it? ).
将应用程序改为使用getTimestamp而不是getObject。这个
在可能的情况下是一个很好的解决方案,但是许多应用程序包含依赖于getObject的
泛型代码,因此并不总是可行。
Alter you application to use getTimestamp rather than getObject. This is a good solution when possible, however many applications contain generic code that relies on getObject, so it isn't always possible.
设置V8Compatible连接属性。这告诉JDBC驱动程序
使用旧映射而不是新映射。您可以将此标志
设置为连接属性或系统属性。您可以通过将
连接属性添加到传递给DriverManager.getConnection或
OracleDataSource.setConnectionProperties的java.util.Properties对象
来设置它。您可以通过在java命令行中包含-D选项来设置系统属性
。
Set the V8Compatible connection property. This tells the JDBC drivers to use the old mapping rather than the new one. You can set this flag either as a connection property or a system property. You set the connection property by adding it to the java.util.Properties object passed to DriverManager.getConnection or to OracleDataSource.setConnectionProperties. You set the system property by including a -D option in your java command line.
java -Doracle.jdbc.V8Compatible =trueMyApp
java -Doracle.jdbc.V8Compatible="true" MyApp
这是链接: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_00
这篇关于引起:java.lang.ClassCastException:java.sql.Timestamp无法强制转换为java.sql.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!