使用java连接数据库以后显示“ Establishing SSL connection without server's identity verification is not recommend...

本文介绍了一个简单的 Java 程序示例,演示如何使用 JDBC 连接 MySQL 数据库并执行 SQL 查询操作。文章提供了完整的代码实现,并解决了运行过程中出现的警告问题。

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

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版本的事情罢了

转载于:https://www.cnblogs.com/mmykdbc/p/8822037.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值