使用java连接数据库以后显示“ Establishing SSL connection without server's identity verification is not recommended”的警告如何解决

今天写了一段查询数据库的操作,如下

package MySQL;

import java.sql.*;

public class MySQL {
    //JDBC驱动名以及数据库URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/test";

    //数据库的用户名和密码
    static final String USER = "root";
    static final String PASS = "0825";

    public static void main(String args[]) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //注册JDBS驱动
            Class.forName("com.mysql.jdbc.Driver");

            //打开连接
            System.out.println("连接数据库");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            //执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "select * from users";
            ResultSet rs = stmt.executeQuery(sql);

            //展开结果集数据库
            while(rs.next()){
                //通过字段见索引
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");

                //输出数据
                System.out.println("Id:"+id);
                System.out.println("名字:"+name);
                System.out.println("年龄"+age);
            }
            //完成后进行关闭
            rs.close();
            stmt.close();
            conn.close();


        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {
            }// 什么都不做

            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
            System.out.println("Goodbye!");
        }
    }
}

 表结构和表内容:

 

 

 好,那么我们来看一下运行的结果:

没有影响显示,但是却出现了警告,网上查询资料,原来是需要在DB_URL后面加上参数:

// 加了 ?useUnicode=true&characterEncoding=utf-8&useSSL=false
 static final String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false";

  OK,成功运行,其实这个警告不影响使用,只是SQL版本的事情罢了

posted @ 2018-04-13 17:24  明明一颗大白菜  阅读(465)  评论(0编辑  收藏  举报
<-- -->