关于spring boot使用 jdbc+mysql 连接的问题

网友投稿 504 2022-08-16


关于spring boot使用 jdbc+mysql 连接的问题

1.创建文件,勾选JDBC和mysql

pom.xml中添加了mysql驱动包和jdbc启动器

2.application.yml添加数据库的配置

spring:

datasource:

username: root

password: 123456

# 针对mysql8版本以上的驱动包,需要指定时区

url: jdbc:mysql://127.0.0.1:3306/jdbc?servehttp://rTimezong=GMT%2B8

# 针对mysql8版本以上的驱动包,需要指定新的驱动类

driver-class-name: com.mysql.cj.jdbc.Driver

mysql 8.x版本驱动包,要使用 com.mysql.cj.jdbc.Driver 作为驱动类

3.测试类中进行测试

package com.cc.springboot;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTeshttp://t;

import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.SQLException;

@SpringBootTest

class Springboot08DataJdbcApplicationTests {

@Autowired

DataSource dataSource;

@Test

void contextLoahttp://ds() throws SQLException {

System.out.println("dataSource:"+dataSource.getClass());

Connection connection = dataSource.getConnection();

System.out.println(connection);

connection.close();

}

}

运行结果:

SpringBoot 默认采用的数据源连接池是:com.zaxxer.hikari.HikariDataSource

数据源相关配置都在 DataSourceProperties 中;

常见错误

The last packet sent successfully to the server was 0http:// milliseconds ago. The driver has not received any packets from the server.

说明mysql服务器没有启动,需要启动mysql服务, 你用navicat连接试试看是否可以连接,不可以说明  没有启动 ;

The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one

时区异常:需要配置文件中指定时区: jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=GMT%2B8


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Nacos客户端配置中心缓存动态更新实现源码
下一篇:深入浅出讲解Spring框架中依赖注入与控制反转及应用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~