zoukankan      html  css  js  c++  java
  • 使用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版本的事情罢了

  • 相关阅读:
    python重载四则运算符及输出格式设置
    hdu 1069 Monkey and Banana
    python特殊函数 __len__(self):
    selective_search_rcnn.m中代码
    用list去初始化numpy的array数组 numpy的array和python中自带的list之间相互转化
    把字符串转化为整数
    这就是那个feature map256 256向量
    对faster rcnn代码讲解的很好的一个
    input_shape { dim: 1 dim: 3 dim: 224 dim: 224 }
    faster rcnn的改进方向
  • 原文地址:https://www.cnblogs.com/mmykdbc/p/8822037.html
Copyright © 2011-2022 走看看