JDBC——setting useSSL=false, or set useSSL=true

今天写了一段代码:出问题了,不要慌,百度,解决了,哈哈。得劲。

 1 package cn.zpoor.DBTest;
 2 
 3 import java.sql.DriverManager;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 
 7 import com.mysql.jdbc.Connection;
 8 import com.mysql.jdbc.PreparedStatement;
 9 
10 public class Students {
11     private int id;
12     private String name;
13     private String sex;
14     private int age;
15     public Students(String name, String sex, int age) {
16         this.id = 0;
17         this.name = name;
18         this.sex = sex;
19         this.age = age;
20     }
21     public int getId() {
22         return id;
23     }
24     public void setId(int id) {
25         this.id = id;
26     }
27     public String getName() {
28         return name;
29     }
30     public void setName(String name) {
31         this.name = name;
32     }
33     public String getSex() {
34         return sex;
35     }
36     public void setSex(String sex) {
37         this.sex = sex;
38     }
39     public int getAge() {
40         return age;
41     }
42     public void setAge(int age) {
43         this.age = age;
44     }
45     
46     
47     private static Connection getConn() {
48         String driver = "com.mysql.jdbc.Driver";
49         String url = "jdbc:mysql://localhost:3306/school";
50         String username = "root";
51         String password = "zhuqiong520";
52         Connection conn = null;
53         try {
54             Class.forName(driver);//加载驱动
55             conn = (Connection) DriverManager.getConnection(url, username, password);
56         } catch (ClassNotFoundException e) {
57             e.printStackTrace();
58         } catch (SQLException e) {
59             e.printStackTrace();
60         }
61         return conn;
62     }
63     
64     private static Integer getAll() {
65         Connection conn = getConn();
66         String sql = "select * from students";
67         PreparedStatement pstmt;
68         
69         try {
70             pstmt = (PreparedStatement) conn.prepareStatement(sql);
71             ResultSet rs = pstmt.executeQuery();
72             int col = rs.getMetaData().getColumnCount();
73             System.out.println("=============");
74             while(rs.next()) {
75                 for(int i = 1;i<=col;i++) {
76                     System.out.println(rs.getString(i)+"	");
77                     if (i==2 && rs.getString(i).length()<8) {
78                         System.out.println("	");
79                     }
80                 }
81                 System.out.println("");
82             }
83             System.out.println("============");
84         } catch (SQLException e) {
85             e.printStackTrace();
86         }
87         return null;
88     }
89     
90     public static void main(String[] args) {
91         Students.getAll();
92     }
93 }
Wed Oct 11 22:20:41 CST 2017 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.
============= 1 朱琼 男 22 2 王珊珊 女 21 ============

用的是java-connector-5.1.42-bin.jar

当然结果是对的,但是上面一行说的什么useSSl没有设置,百度了一下,是这样的。

  冷静分析:主要是我的jar包版本过高造成的。用以前的旧版本没什么问题,而且新版本的MySQL要求是否进行ssl连接。

  解决方法:这样写就可以了  String url = "jdbc:mysql://localhost:3306/school?useSSL=false";   

拓展:conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test_db?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","password");

   useUnicode设置主要是设置字符编码为utf-8,也可以在eclipse中设置默认的字符编码。

        

原文地址:https://www.cnblogs.com/zpoor/p/7653455.html