jdbc 06: 实现登陆页面

jdbc连接mysql,实现简单的登陆验证

package com.examples.jdbc.o6_实现登录界面;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Scanner;

/*
    jdbc实现用户登录界面
 */
public class Test {
    public static void main(String[] args) {
        //用户登录
        Map<String, String> userInfo = userLogin();
        //验证登录信息
        boolean loginResult = loginCheck(userInfo);
        System.out.println(loginResult ? "登录成功" : "登录失败");
    }

    /**
     * 登录信息验证
     * @param userInfo 用户输入的用户名,密码
     * @return 登录信息检测结果,通过验证:true, 未通过验证:false
     */
    private static boolean loginCheck(Map<String, String> userInfo) {
        boolean loginResult = false;
        String name = userInfo.get("userName");
        String passwd = userInfo.get("passWord");

        //jdbc连接数据库进行登录验证

        //资源绑定器绑定数据库配置信息
        ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String userName = resourceBundle.getString("userName");
        String passWord = resourceBundle.getString("passWord");

        //3个资源
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //1.
            Class.forName(driver);
            //2.
            connection = DriverManager.getConnection(url, userName, passWord);
            //3.
            statement = connection.createStatement();
            //4.
            String sql = "select * from tb_user where uname = '"+name+"' and upasswd = '"+passwd+"'";
            resultSet = statement.executeQuery(sql);
            if(resultSet.next()){
                loginResult = true;
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            //6.
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return loginResult;
    }


    /**
     * 用户登录
     * @return 用户名,用户密码
     */
    private static Map<String, String> userLogin() {
        Scanner scanner = new Scanner(System.in);

        System.out.println("用户名: ");
        String userName = scanner.nextLine();
        System.out.println("密码: ");
        String passWord = scanner.nextLine();

        Map<String, String> userInfo = new HashMap<>();
        userInfo.put("userName", userName);
        userInfo.put("passWord", passWord);

        return userInfo;
    }
}
版权声明:程序员胖胖胖虎阿 发表于 2022年9月1日 上午2:32。
转载请注明:jdbc 06: 实现登陆页面 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...