简体   繁体   English

引起: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.
        }
      }

Error: 错误:

Caused by: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.sql.Date

In the database, the datatype for the column is DATE only. 在数据库中,该列的数据类型仅为DATE I am not able to figure out where the Timestamp is coming here. 我无法弄清楚Timestamp在这里的位置。

Obsolete: 过时:

Use java.util.Date for the field. java.util.Date用于该字段。 java.sql.Timestamp is a direct subclass of it. java.sql.Timestamp是它的直接子类。 As is java.sql.Date - that strips the time part. java.sql.Date - 剥离时间部分。 Why the java database driver takes DATE to be Timestamp is a bit weird. 为什么java数据库驱动程序将DATE设为Timestamp有点奇怪。 What is the database vendor? 什么是数据库供应商? Did you specify a length or so? 你指定了一个长度吗? Are indeed only dates stored? 确实只存储日期?


Researched: 研究:

I looked into CachedRowSetImpl.java , and Oracle's docs and Oracle does everything fine (java.sql.Date, java.sql.Time, java.sql.Timestamp convertible). 我查看了CachedRowSetImpl.java ,Oracle的文档和Oracle做的一切都很好(java.sql.Date,java.sql.Time,java.sql.Timestamp convertible)。 The CachedRowSetImpl does simply cast the DATE's Object (and getObject is likely to return the high resolution Timestamp - with time) to java.sql.Date, and that's wrong. CachedRowSetImpl只是简单地转换DATE的对象(并且getObject可能会将高分辨率时间戳 - 随时间返回)返回java.sql.Date,这是错误的。 So override or substitute this sun's class. 所以重写替换这个太阳的阶级。

      /*
       * The object coming back from the db could be
       * a date, a timestamp, or a char field variety.
       * If it's a date type return it, a timestamp
       * we turn into a long and then into a date,
       * char strings we try to parse. Yuck.
       */
       switch (RowSetMD.getColumnType(columnIndex)) {
           case java.sql.Types.DATE: {
               long sec = ((java.sql.Date)value).getTime();
               return new java.sql.Date(sec);
       }

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. 我发现DATE和TIMESTAMP之间的这种混淆是特定于JDBC驱动程序的。 And most of the links suggest the use of -Doracle.jdbc.V8Compatible=true . 大多数链接建议使用-Doracle.jdbc.V8Compatible=true For my JBoss I have set this in run.bat and the issue got resolved. 对于我的JBoss,我在run.bat设置了它,问题得到了解决。

  1. https://community.oracle.com/thread/68918?start=0&tstart=0 https://community.oracle.com/thread/68918?start=0&tstart=0

  2. http://www.coderanch.com/t/90891/JBoss/oracle-jdbc-Compatible-true http://www.coderanch.com/t/90891/JBoss/oracle-jdbc-Compatible-true

  3. https://community.oracle.com/message/3613155 https://community.oracle.com/message/3613155

The oracle doc shares different solutions: oracle doc分享不同的解决方案:

  • Alter your tables to use TIMESTAMP instead of DATE. 更改表以使用TIMESTAMP而不是DATE。 This is probably rarely possible, but it is the best solution when it is. 这可能很少可能,但它是最好的解决方案。

  • Alter your application to use defineColumnType to define the columns as TIMESTAMP rather than DATE. 更改应用程序以使用defineColumnType将列定义为TIMESTAMP而不是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? ). 这有问题因为你真的不想使用defineColumnType,除非你必须(参见什么是defineColumnType以及何时应该使用它?)。

  • Alter you application to use getTimestamp rather than getObject. 改变应用程序使用getTimestamp而不是getObject。 This is a good solution when possible, however many applications contain generic code that relies on getObject, so it isn't always possible. 在可能的情况下,这是一个很好的解决方案,但是许多应用程序包含依赖于getObject的通用代码,因此并不总是可行。

  • Set the V8Compatible connection property. 设置V8Compatible连接属性。 This tells the JDBC drivers to use the old mapping rather than the new one. 这告诉JDBC驱动程序使用旧映射而不是新映射。 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. 您可以通过将连接属性添加到传递给DriverManager.getConnection或OracleDataSource.setConnectionProperties的java.util.Properties对象来设置连接属性。 You set the system property by including a -D option in your java command line. 您可以通过在java命令行中包含-D选项来设置系统属性。

    java -Doracle.jdbc.V8Compatible="true" MyApp java -Doracle.jdbc.V8Compatible =“true”MyApp

Here is the link: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_00 这是链接: http//www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_00

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 java.lang.ClassCastException: java.lang.String 无法转换为 java.sql.Timestamp - getting java.lang.ClassCastException: java.lang.String cannot be cast to java.sql.Timestamp java.lang.ClassCastException: class java.sql.Date cannot be cast to java.lang.String - java.lang.ClassCastException: class java.sql.Date cannot be cast to java.lang.String 错误 - 无法在JFreeChart中将java.sql.Timestamp强制转换为java.sql.Date - Error - java.sql.Timestamp cannot be cast to java.sql.Date in JFreeChart 将java.sql.Date转换为java.sql.Timestamp - Convert java.sql.Date to java.sql.Timestamp 将java.sql.Timestamp转换为java.sql.Date - convert java.sql.Timestamp to java.sql.Date 获取 java.lang.ClassCastException:java.lang.String 无法转换为 java.sql.Clob - Getting the java.lang.ClassCastException: java.lang.String cannot be cast to java.sql.Clob ClassCastException:无法将java.util.Date强制转换为java.sql.Date - ClassCastException: java.util.Date cannot be cast to java.sql.Date oracle.sql.TIMESTAMP无法强制转换为java.sql.Timestamp - oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp 原因:java.lang.ClassCastException:org.apache.commons.dbcp.PoolingDataSource无法转换为javax.sql.DataSource - Caused by: java.lang.ClassCastException: org.apache.commons.dbcp.PoolingDataSource cannot be cast to javax.sql.DataSource For java.sql.Date AND java.sql.timestamp, can both be converted to datetime in sql server? - For java.sql.Date AND java.sql.timestamp, can both be converted to datetime in sql server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM