使用JDBC实现简单登录验证

14 篇文章 1 订阅
import java.sql.*;
import java.util.*;

/**
 * 类描述:使用PrepareStatement实现登录验证
 * Author: WuChu
 * Date: 2021/8/2 22:53
 * Version: 1.0
 */
public class PrepareStatementTest {
    public static void main(String[] args) {
        //初始化界面
        //新建一个用户信息Map=集合对象
        Map<String,String> userInfo = Init();

        //判断是否合法
        boolean ok = CheckLogin(userInfo.get("uName"),userInfo.get("pWord"));

        //输出结果
        System.out.println(ok?"登录成功!":"账号或密码错误!");

    }


    /**
     * 初始化界面
     * @return  userInfo
     */
    private static Map<String,String> Init(){
        System.out.println("********欢迎使用系统,请输入账号密码确认身份*******");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入账号:");
        String uName = scanner.next();
        System.out.print("请输入密码:");
        String pWord = scanner.next();

        //将用户输入的用户名和密码放在Map集合中
        Map<String,String> userInfo = new HashMap<>();
        userInfo.put("uName",uName);
        userInfo.put("pWord",pWord);

        //返回Map集合
        return userInfo;

    }

    /**
     * 连接数据库,判断账号密码是否合法
     * @param userName 用户输入的账号
     * @param passWord 用户输入的密码
     * @return TRUE or FALSE
     */
    private static boolean CheckLogin(String userName,String passWord){

        //先定义一些全局变量
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs ;
        boolean ok = false;

        //资源绑定器
        ResourceBundle resourceBundle = ResourceBundle.getBundle("userInfo");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String username = resourceBundle.getString("username");
        String password = resourceBundle.getString("password");

        //连接数据库
        try {
            //1、注册数据库驱动
            Class.forName(driver);

            //2、获取数据库连接
            connection = DriverManager.getConnection(url,username,password);

            //3、获取预编译的数据库操作对象
            String sql = "select * from user where username = '" + userName+"' and password = '"+ passWord +"'";
            ps = connection.prepareStatement(sql);

            //4、执行SQL语句
            rs = ps.executeQuery();

            if(rs.next()){
                ok = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            //6、关闭连接
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException se) {
                    se.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException se) {
                    se.printStackTrace();
                }
            }
        }
        return ok;
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用JDBC(Java Database Connectivity)来实现登录判断。下面是一个简单的示例代码: ```java import java.sql.*; public class LoginExample { public static void main(String[] args) { // 数据库连接信息 String url = "jdbc:mysql://localhost:3306/mydatabase"; String username = "root"; String password = "password"; // 用户输入的用户名和密码 String inputUsername = "testuser"; String inputPassword = "testpassword"; try { // 加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立数据库连接 Connection conn = DriverManager.getConnection(url, username, password); // 创建SQL语句 String sql = "SELECT * FROM users WHERE username = ? AND password = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, inputUsername); stmt.setString(2, inputPassword); // 执行查询 ResultSet rs = stmt.executeQuery(); if (rs.next()) { System.out.println("登录成功!"); } else { System.out.println("用户名或密码错误!"); } // 关闭数据库连接 rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 上述代码假设你已经安装了 MySQL 数据库并且在本地运行在默认端口上。你需要将 `mydatabase` 替换为你实际的数据库名称,并且提供正确的数据库用户名和密码。 代码中使用了预处理语句来防止 SQL 注入攻击。它将输入的用户名和密码与数据库中的对应记录进行比较,如果存在匹配的记录,则登录成功,否则登录失败。 请注意,上述示例仅为演示目的,实际应用中应该更加严格地进行用户身份验证和密码存储。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梧杵

还是学生,生活太难

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值