JDBC的复习(二):使用IDEA开发

6 篇文章 0 订阅

JDBC的复习(二):使用IDEA开发

配置驱动

  1. 创建项目
  2. 打开Module setting

image-20220227223305283

  1. 点击+号后找到自己的数据库架包

    没有的话可以到官网下载:

image-20220227223455450

image-20220227223516034

  1. 点击ok

image-20220227223608155

  1. 显示就成功

image-20220227223652476

测试是否导入:

package com.bjpowernode.jdb;

/**
 * @author 31200
 */
public class JdbcTest06 {
    public static void main(String[] args) {
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}

导入成功!!!

创建项目

怎样创建目录的时候自动创建子级包?

参考这个博客:

https://blog.csdn.net/sinat_36158016/article/details/107423723

创建数据库表

image-20220228124410282

模拟用户登陆

  1. main函数
public static void main(String[] args) {
        //初始化用户界面
        Map<String,String> userLoginInfo = InitUI();

        //登陆函数
        boolean flag = Login(userLoginInfo);

        //输出结果
        System.out.println(flag? "登陆成功!":"登陆失败!");

    }
  1. 初始化UI界面
/**
     * 初始化用户界面
     * @return 用户输入的用户名和密码登陆信息
     */
    private static Map<String, String> InitUI() {
        Map<String,String> userLoginInfo = new HashMap<>();
        Scanner s = new Scanner(System.in);

        System.out.println("用户名:");
        String userName = s.nextLine();

        System.out.println("密码:");
        String passWord = s.nextLine();
        userLoginInfo.put("useName",userName);
        userLoginInfo.put("passWord",passWord);

        return userLoginInfo;
    }
  1. 登录函数
/**
     * 登陆函数,判断登陆是否成功
     * @param userLoginInfo
     * @return 返回登陆是否成功
     */
    private static boolean Login(Map<String, String> userLoginInfo) {
        boolean flag = false;

        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;

        String userName = userLoginInfo.get("useName");
        String passWord = userLoginInfo.get("passWord");

        try {
            //1. 注册驱动

            Class.forName("com.mysql.jdbc.Driver");
            //2. 获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?characterEncoding=UTF8&useSSL=false","root","001204");
            //3. 获取操作对象
            stat = conn.createStatement();
            //4. 执行sql语句
            String sql = "select * from userinfo where userName = '"+userName+"' and passWord = '"+passWord+"'";
            rs = stat.executeQuery(sql);
            //5. 处理查询结果集合
            if(rs.next()){
                flag = true;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {

            try {
                //释放资源
                if(rs!=null){
                    rs.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                //释放资源
                if(stat!=null){
                    stat.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                //释放资源
                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }

        return flag;
    }

注意:如果报错 WARN: Establishing SSL connection without server's identity verification is not recommended.

Mon Feb 28 12:33:02 CST 2022 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.

解释:

周一2月28日12:33:02 CST 2022警告:不建议在没有服务器身份验证的情况下建立SSL连接。根据MySQL 5.5.45+、5.6.26+和5.7.6+的要求,如果未设置显式选项,默认情况下必须建立SSL连接。为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为“false”。您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任库。

解决方案:

在连接数据库的配置后面增加useSSL=false。

image-20220228124556051

测试成功!!

image-20220228124907631

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值