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

  • Post author:
  • Post category:java





环境

1、数据库版本:

mysql5.7

2、数据库链接驱动Jar包:

mysql-connector-java-8.0.26.jar




场景

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




报错信息

java.lang.ClassCastException: java.time.LocalDateTime cannot be cast to java.util.Date
	at 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

或更为早期的版本



补充

1、

mysql-connector-java-8.0.22.jar

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

2、

mysql-connector-java-8.0.22.jar

pom.xml坐标如下

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



版权声明:本文为weixin_52116015原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。