返回顶部
首页 > 资讯 > 数据库 >mysql查询报错:java.lang.ClassCastException: java.time.LocalDateTime cannot be cast to java.util.Date
  • 273
分享到

mysql查询报错:java.lang.ClassCastException: java.time.LocalDateTime cannot be cast to java.util.Date

mysqljava数据库 2023-09-30 15:09:47 273人浏览 八月长安
摘要

目录 环境场景报错信息原因解决补充 环境 数据库版本:Mysql5.7 数据库链接驱动jar包:mysql-connector-java-8.0.26.jar 场景 当sel

目录


环境

数据库版本:Mysql5.7

数据库链接驱动jar包:mysql-connector-java-8.0.26.jar


场景

当select语句查询日期类型的字段,在映射结果对象时报错


报错信息

java.lang.ClassCastException: java.time.LocalDateTime cannot be cast to java.util.Dateat com.xxx.dao.AuthTokenDao.find(AuthTokenDao.java:40)at com.xxx.Listener.MyNewListener.contextInitialized(MyNewListener.java:317)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4716)at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5177)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:706)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1133)at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1866)at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at org.apache.Tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1045)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:429)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1576)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:309)at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:936)at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:843)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)at org.apache.catalina.startup.Catalina.start(Catalina.java:772)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:342)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:473)

原因

通过百度,说是mysql-connector-java-8.0.22.jar以上版本的mysql依赖,返回的日期类型就是LocalDataTime,并且在强转时会报错(未确认)


解决

将高于8.0.22以上的版本的mysql驱动更换为mysql-connector-java-8.0.22.jar或更为早期的版本

补充

mysql-connector-java-8.0.22.jar是兼容mysql5和mysql8数据库的,在查询日期类型的字段时可以正常映射结果类

mysql-connector-java-8.0.22.jarpom.xml坐标如下

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.22</version></dependency>

来源地址:https://blog.csdn.net/weixin_52116015/article/details/131557361

您可能感兴趣的文档:

--结束END--

本文标题: mysql查询报错:java.lang.ClassCastException: java.time.LocalDateTime cannot be cast to java.util.Date

本文链接: https://lsjlt.com/news/421937.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作