zoukankan      html  css  js  c++  java
  • 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中设置默认的字符编码。

            

  • 相关阅读:
    文本比较算法Ⅴ——回顾贴,对前面几篇文章的回顾与质疑
    键盘监控的实现Ⅱ——容易产生误解的CallNextHookEx函数
    利用WPF建立自适应窗口大小布局的WinForm窗口
    计算机中的颜色XIII——颜色转换的快速计算公式
    键盘监控的实现Ⅲ——按键消息的修改(映射)
    计算机中的颜色XI——从色相值到纯色的快速计算(新的公式)
    Dot Net中InputLanguage对象的使用限制
    计算机中的颜色XII——快速计算纯色的色相值(新的公式)
    关于房产中介网的设计随想
    java笔记:熟练掌握线程技术基础篇之解决资源共享的问题(中)前篇
  • 原文地址:https://www.cnblogs.com/zpoor/p/7653455.html
Copyright © 2011-2022 走看看