简体   繁体   English

登录系统 JSF - 识别出 SQLException?

[英]Login System JSF - Identified an SQLException?

Well thanks to the people who actually tried helping me but I found out what my problem was in the end: MYSQL script caused it to search for "Admin1" which is my login id when it should have searched username and password so had to modify my query to do so.好吧,感谢那些真正尝试帮助我的人,但我最终发现了我的问题:MYSQL 脚本导致它搜索“Admin1”,这是我的登录 ID,而它应该搜索用户名和密码,因此不得不修改我的查询这样做。 I'll leave my solution for future apprentices.我会把我的解决方案留给未来的学徒。

    public void dbData(String UName, String PWord) {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/unidb?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "Safia10122014");
        String SQL = "select `Username`, `Password` from `SPAS_Login_Details` where `Username` like 'MWR0025'";

        Statement statement = con.createStatement();

        ResultSet resultSet = statement.executeQuery(SQL);
        resultSet.next();
        dbUname = resultSet.getString(1);
        dbPword = resultSet.getString(2);

    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
        System.out.println(e);
    }
}

public String validation() {
    dbData(name, password);

    if (name.equalsIgnoreCase(dbUname) && password.equalsIgnoreCase(dbPword)) {
        return "Admin_home";
    } else {
        return "";
    }
}

Your codes look fine, i think the reason why you are not directed to the Admin_ home is because you haven't called the "checkValidUser" function.你的代码看起来不错,我认为你没有被定向到 Admin_home 的原因是因为你没有调用“checkValidUser”函​​数。 It should be called here这里应该叫

if(rs.next()){ checkValidUser():} if(rs.next()){ checkValidUser():}

if no errors are found, the program should work correctly.如果没有发现错误,程序应该可以正常工作。

As seen in the edited post:如编辑后的帖子所示:

    public void dbData(String UName, String PWord) {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/unidb?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "Safia10122014");
        String SQL = "select `Username`, `Password` from `SPAS_Login_Details` where `Username` like 'MWR0025'";

        //String SQL = "select * from `SPAS_Login_Details` where `Username` = 'MWR0025' and `Password` = 'MWR0025'";
        //String SQLA = "select * from `SPAS_Login_Details` where `Username` = 'QD0040' and `Password` = 'QD0040'";
        //String SQLB = "select * from `SPAS_Login_Details` where `Username` = 'HB0041' and `Password` = 'HB0041'";
        //String SQLC = "select * from `SPAS_Login_Details` where `Username` = 'VV0042' and `Password` = 'VV0042'";
        //String SQLD = "select * from `SPAS_Login_Details` where `Username` = 'MA0043' and `Password` = 'MA0043'";
        //String SQLE = "select * from `SPAS_Login_Details` where `Username` = 'NF0044' and `Password` = 'NF0044'";

        Statement statement = con.createStatement();

        ResultSet resultSet = statement.executeQuery(SQL);
        resultSet.next();
        dbUname = resultSet.getString(1);
        dbPword = resultSet.getString(2);

    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
        System.out.println(e);
    }
}

public String validation() {
    dbData(name, password);

    if (name.equalsIgnoreCase(dbUname) && password.equalsIgnoreCase(dbPword)) {
        return "Admin_home";
    } else {
        return "";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM