You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Spring Boot应用时MySQL驱动与JDBC报错原因咨询

报错原因及解决思路

报错信息

"Caused by: java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl, mysql://localhost:3306"

"Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl, mysql://localhost:3306"

核心原因

  1. JDBC URL前缀错误
    MySQL Connector/J 8.x版本驱动要求JDBC URL必须以jdbc:mysql://开头,你当前配置的mysql://localhost:3306缺少jdbc:前缀,这是驱动拒绝该URL的直接原因。

  2. URL未指定目标数据库
    正确的MySQL JDBC URL需要明确指向具体数据库名称,仅写localhost:3306没有指定要连接的数据库,驱动无法完成有效连接。

  3. (可选但建议)缺少时区参数
    MySQL 8.x驱动默认使用UTC时区,若你的数据库服务器是国内时区(如Asia/Shanghai),未指定时区会引发额外兼容性问题,建议补充该参数。

正确配置示例

以Spring Boot的application.properties为例,标准数据源配置如下:

spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名称?serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=你的数据库用户名
spring.datasource.password=你的数据库密码

内容的提问来源于stack exchange,提问作者Zacharie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 13:01:11