1.javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify的解决办法
配置连接数据库的url时,加上useSSL=false。如以下格式,注意将数据库名(db_testjpa)改为你自己的数据库名。
spring.datasource.url = jdbc:mysql://localhost:3306/db_testjpa?serverTimezone=GMT%2B8&useSSL=false
2.关于 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.
根据警告说明数据库驱动采用的是新的驱动器SPI机制加载的, 解决办法
1.驱动的全限定类名: 包名 + 类名, 改为com.mysql.cj.jdbc.Driver
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
2.链接url的时区必须加上, 需要用到的地方都要加上
spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
3.com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to
场景:本地用Navicat For mysql连接没问题,但是在java项目中连接不上,最后发现原因是我的mysql的版本是8.0.22,而mysql数据库驱动包版本是5.1.36
两者版本不兼容所以连不上,解决思路:将mysql版本降低或者使用更高版本的mysql数据库驱动包。
解决:
第一步:更高版本的数据库驱动包:
第二步:修改驱动类名和url
其中useSSL=false不加会报SSL异常:
serverTimezone=Asia/Shanghai时区也要指定,不然会报错
其他几个配置项到时没有说明必填。
附:java连接数据库测试代码
package com.javen;
import com.javen.util.DBHelper;
import java.sql.*;
public class TestMysqlConnecttion {
public static void main(String[] args) {
//声明Connection对象
Connection con;
//驱动程序名
//String driver = "com.mysql.jdbc.Driver";
String driver = "com.mysql.cj.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/hzz_dev?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "123321";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//要执行的SQL语句
String sql = "select * from student";
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" ID" + "\t" + " 姓名");
System.out.println("-----------------");
String name = null;
String id = null;
while(rs.next()){
name = rs.getString("id");
id = rs.getString("name");
//首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
//然后使用GB2312字符集解码指定的字节数组。
//输出结果
System.out.println(name + "\t" + id);
}
//测试修改
String sqlUpdate = "update student set name = concat(name , 'copy')";
//3.ResultSet类,用来存放获取的结果集!!
statement.executeUpdate(sqlUpdate);
sql = "select * from student";
//3.ResultSet类,用来存放获取的结果集!!
rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" ID" + "\t" + " 姓名");
System.out.println("-----------------");
name = null;
id = null;
while(rs.next()){
name = rs.getString("id");
id = rs.getString("name");
//首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
//然后使用GB2312字符集解码指定的字节数组。
//输出结果
System.out.println(name + "\t" + id);
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}
}