Java连接mysql-8.0.11出现的问题
今天学习数据库JDBC连接,写好程序运行出现以下警告:
WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
百度后发现原因是MySQL在高版本需要指明是否进行SSL连接
解决方案:
在url中添加&useSSL=false或者添加&useSSL=true
“jdbc:mysql://localhost:3306/ 数据库名 ?useSSL=false&serverTimezone=UTC”, “root”, “root”
问题解决
mysql-8.0.11连接驱动:com.mysql.cj.jdbc.Driver
代码部分:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79package net.zzqd.login; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;public class LoginDemo
{ public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = input.nextLine();
System.out.println("请输入密码:");
String pass = input.nextLine();
//连接对象
Connection connection = null;
//命令对象
Statement stat = null;
//结果集
ResultSet rs = null;
//操作数据库
//1.注册驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//2获取连接
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/school?useSSL=false&serverTimezone=UTC", "root", "root");
System.out.println("-------------------------------------");
//3创建命令
stat = connection.createStatement();
//执行命令
rs = stat.executeQuery("select * from user where username='"+username+"' and password='"+pass+"'");
if(rs.next())
{
//有数据
System.out.println("登陆成功");
}
else
{
System.out.println("账号或密码错误");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally
{
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stat!=null)
{
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null)
{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}