1. 程式人生 > >JDBC——setting useSSL=false, or set useSSL=true

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

try rest pri while localhost disable url ssl driver

今天寫了一段代碼:出問題了,不要慌,百度,解決了,哈哈。得勁。

 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)+"\t"); 77 if (i==2 && rs.getString(i).length()<8) { 78 System.out.println("\t"); 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中設置默認的字符編碼。

        

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