MySQL数据库的datetime类型的字段对应Java的java.util.Date和java.time.LocalDateTime数据类型

最近在开发JavaWeb项目时,使用C3P0连接方式和DBUtil工具类访问MySQL数据库,MySQL数据库版本5.7.33

出现错误:

java.sql.SQLException: Cannot set collectTime: incompatible types, cannot convert java.time.LocalDateTime to java.util.Date

java.sql.SQLException: Cannot set collectTime: incompatible types, cannot convert java.sql.TimeStamp to java.time.LocalDateTime

困扰很久,最后找到问题所在----驱动版本对应不对。

一、MySQL数据库的datetime类型的字段对应Java的java.util.Date数据类型

在JavaWeb项目中使用mysql的连接器驱动版本应该是5.1.49版本:mysql-connector-java-5.1.49.jar

驱动类名:com.mysql.jdbc.Driver

c3p0-config.xml文件代码:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/db_health?useSSL=false</property>
        <property name="user">root</property>
        <property name="password">1234</property>
    </default-config> 
</c3p0-config> 
复制代码

 

 

 

 

二、MySQL数据库的datetime类型的字段对应java.time.LocalDateTime数据类型

java.time.LocalDateTime是Java 8 的新增日期时间类。

在JavaWeb项目中使用mysql的连接器驱动版本应该是8.0.82版本:mysql-connector-java-8.0.28.jar

驱动类名:com.mysql.cj.jdbc.Driver

c3p0-config.xml文件代码:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/db_health?useSSL=false</property>
        <property name="user">root</property>
        <property name="password">1234</property>
    </default-config> 
</c3p0-config>
复制代码

 

posted @   熊猫Panda先生  阅读(10990)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示