Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

时间:2022-01-30 11:05:03

1. 数据库

1.1 创建表

在当前数据库students中,创建数据表student:

 mysql> create table student(
-> studentid char(10),#学生ID
-> name varchar(10),#学生姓名
-> age smallint,#学生年龄
-> grade smallint)#成绩
-> ;

1.2 插入信息:

INSERT student VALUES("1*********","tom",18,68)

插入后是这样:

Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

2. 下载connector并导入

2.1 下载解压

在 https://dev.mysql.com/downloads/connector/j/ 下载mysql的jdbc驱动器

解压后:

Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

.jar文件为我们需要的文件。

2.2 创建项目导入jar包

eclipse IDE创建Java项目,右击Java项目:

Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

选择configure Build path

Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

两种方式添加jar文件都能实现。

其中区别在于:

add jars需要事先将jar文件复制到该项目里面来,点击add jars按钮之后,在该项目里选择该jar文件。使用的是相对路径,即使把该项目复制到别的电脑也可以使用。

add external Jars 则是用的绝对路径,在资源管理器里面选择jar文件。并且之后会出现:

Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

3. 代码

3.1 编写代码:

import java.sql.*;
import com.mysql.*;
public class Test1 { public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?useSSL=true","root","11111");
java.sql.Statement sql=conn.createStatement();
java.sql.ResultSet rs=sql.executeQuery("select * from student");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
}
} }

3.2 代码讲解:

  • Class.forName() 是用来加载驱动。Class.forName()的用法详细介绍:http://blog.csdn.net/kaiwii/article/details/7405761
  • getConnection()的用法:

Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

参数url, jdbc:mysql://localhost:3306/students

注意端口号是默认的端口号,如果更改了,此处也要更改。斜杠右边是数据库名称

之所以代码里面后面加? useSSL=true,是因为如果不加,会出现如下警告:

 Warning: Establishing SSL connection without server's identity verification is not recommended

  回答见*: http://*.com/questions/34224970/warning-establishing-ssl-connection-without-servers-identity-verification-is-n

user就是用户名,password密码。

也可以这样:

Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

jdbc:mysql://localhost:3306/jspstudy?user=root&password=111111

  

查询的结果在rs对象里面,通过里面的getString()方法来得到字符串输出,参数里面的整数是列数。

代码和查询结果:

Java连接MySQL Warning: Establishing SSL connection without server's identity verification is not recommended