1.创建文件,勾选JDBC和Mysql pom.xml中添加了mysql驱动包和jdbc启动器 2.application.yml添加数据库的配置 spring: dataso
pom.xml中添加了mysql驱动包和jdbc启动器
spring:
datasource:
username: root
passWord: 123456
# 针对mysql8版本以上的驱动包,需要指定时区
url: jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezong=GMT%2B8
# 针对mysql8版本以上的驱动包,需要指定新的驱动类
driver-class-name: com.mysql.cj.jdbc.Driver
mysql 8.x版本驱动包,要使用 com.mysql.cj.jdbc.Driver 作为驱动类
package com.cc.SpringBoot;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class Springboot08DataJdbcApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() 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 0 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
到此这篇关于spring boot使用 jdbc+mysql 连接的文章就介绍到这了,更多相关spring boot jdb mysql连接内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 关于spring boot使用 jdbc+mysql 连接的问题
本文链接: https://www.lsjlt.com/news/144590.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0