Spring Boot 连接 MySQL 数据库

application.properties文件配置:

#配置数据库
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/demo01
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driver-class-name= com.mysql.jdbc.Driver

参考:常用数据库JDBC URL格式

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

//错误
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

原因:MySQL连接驱动更新了。

//异常
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time 

原因:未指定时区serverTimezone,GMT%2B6为东八区,即中国时区。

修改后配置为:

#配置数据库
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/demo01?serverTimezone=GMT%2B8
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄