首页 > 解决方案 > 一次登录不能使用多个用户

问题描述

我的 mysql 数据库中有 2 个数据库,当我输入第一行是“用户名”并且密码是“密码”时,它会相应地进行。但是当我进入第二行时,它说密码错误

这是我的代码:

public void tlogin() {
    String u,p;
    try {
        String username,password;
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/subject_db","root","");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM teacher_login");
        while(rs.next()) {
            System.out.println("Enter Username: ");
            u =new Scanner(System.in).nextLine();
            System.out.println("Enter Password: ");
            p =new Scanner(System.in).nextLine();
            while(!(u.equals(username = rs.getString(2))&&p.equals(password = rs.getString(3)))) {
                System.out.println("Incorrect Login!");
                System.out.println("Enter Username: ");
                u =new Scanner(System.in).nextLine();
                System.out.println("Enter Password: ");
                p =new Scanner(System.in).nextLine();
            }
            System.out.println("Correct Login!");
            TimeUnit.SECONDS.sleep(1);
            thome();
        }
        con.close();
    }
    catch(Exception e) {
        System.out.println(e);
    }
}

标签: javamysqlauthenticationjdbcresultset

解决方案


推荐阅读