Problem connecting a login method in my Java program to MySQL table using JDBC

huangapple 未分类评论45阅读模式
英文:

Problem connecting a login method in my Java program to MySQL table using JDBC

问题

我正在编写一个Java程序(使用Java Swing和JDBC),在其中一个面板中,我有一个登录界面。我希望我的程序可以比较输入的用户名和密码,并将其与我在数据库表(MySQL)中拥有的用户名和密码进行比较。以下是我在“登录”按钮中编写的代码,您可以自行查看:

try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String url2 = "jdbc:mysql://localhost:3306/Students?user=root";
    Connection connect2 = DriverManager.getConnection(url2);
    Statement state2 = connect2.createStatement();
    String query2 = "select * from login_data where username='%s'";
    query2 = String.format(query2, username1.getText());
    ResultSet result2 = state2.executeQuery(query2);
    if (result2.next()) {
        if (result2.getString("password").equals(password1.getText())) {
            login.setVisible(false);
            Mainmenu();
        } else {
            result1.setText("Wrong Password");
        }
    } else {
        result1.setText("Wrong Username!!!");
    }

    state2.close();
    connect2.close();
} catch (SQLException e1) {
    e1.printStackTrace();
} catch (IllegalAccessException e1) {
    e1.printStackTrace();
} catch (InstantiationException e1) {
    e1.printStackTrace();
} catch (ClassNotFoundException e1) {
    e1.printStackTrace();
}

现在,如果我提供了错误的用户名,它会正确显示“错误的用户名”消息。然而,如果用户名是正确的,它会显示“错误的密码”消息。无论密码是否正确都会如此。

我感觉这里有一个逻辑错误,但我看不到它。

英文:

I'm writing a Java program (with Java Swing and JDBC) and in one of my panels, I have a login. I want my program to compare the entered username and password and compare it to the ones U have in my database table (MySQL). This is what I wrote in my "Login" button, see for yourself:

try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String url2 = "jdbc:mysql://localhost:3306/Students?user=root";
    Connection connect2 = DriverManager.getConnection(url2);
    Statement state2 = connect2.createStatement();
    String query2 = "select * from login_data where username='%s'";
    query2 = String.format(query2,username1.getText());
    ResultSet result2 = state2.executeQuery(query2);
    if(result2.next()){
        if(result2.getString("password")==password1.getText()){
            login.setVisible(false);
            Mainmenu();
        }else{
            result1.setText("Wrong Password");
        }
    }else{
        result1.setText("Wrong Username!!!");
    }

    state2.close();
    connect2.close();

} catch (SQLException e1) {
    e1.printStackTrace();
} catch (IllegalAccessException e1) {
    e1.printStackTrace();
} catch (InstantiationException e1) {
    e1.printStackTrace();
} catch (ClassNotFoundException e1) {
    e1.printStackTrace();
}

Now, if I give it a wrong username, it correctly shows the "wrong username" message. However, if the username is correct, it shows the "wrong password" message. It doesn't matter if the password is/isn't correct.

I feel there is a logical error here, but I can't see it.

huangapple
  • 本文由 发表于 2020年7月27日 16:29:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/63111455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定