user3138212
user3138212

Reputation: 221

Java else statement error

The JOptionPane box keeps appearing at least 3 times if I input the incorrect username and password. Firstly, is it even a while loop.

I spent hours looking at the code and trying to modify it but no luck. Couldn't get much help either, but I would greatly appreciate it if someone could point out or fix my error.

The code where I am getting problems is below.

       try{
        PreparedStatement ps = con.prepareStatement("SELECT Login, Password, Restriction FROM Users");
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            if (username.equals(rs.getString("Login")) && password.equals(rs.getString("Password")) && ("Admin".equals(rs.getString("Restriction")))){
                this.setVisible(false);
                MainMenuAdmin admin = new MainMenuAdmin();
                admin.setVisible(true);
            }
            else if (username.equals(rs.getString("Login")) && password.equals(rs.getString("Password")) && ("Engineer".equals(rs.getString("Restriction")))){
                this.setVisible(false);
                MainMenuEngineer engineer = new MainMenuEngineer();
                engineer.setVisible(true);
            }
            else if (!username.equals(rs.getString("Login")) || !password.equals(rs.getString("Password"))){
              JOptionPane.showMessageDialog(null, "Login Failed");
            }
        }  
    }
    catch(SQLException sqle){
        sqle.printStackTrace();
    }

Also how do code it such that if a username or password doesn't exist, it says 'Username or Password doesn't exist'?

Upvotes: 0

Views: 134

Answers (2)

Boris the Spider
Boris the Spider

Reputation: 61148

You approach is completely wrong.

  1. You should actually query the data. Use the database that you have.
  2. You should close() your resources when are done with them.

This is a method to check the login details. It queries the username and password from the database.

public String checkLogin(final Connection con, final String username, final String password) throws SQLException {
    final String q = "SELECT Restriction FROM Users WHERE Login = ? AND Password = ?";
    try (final PreparedStatement ps = con.prepareStatement(q)) {
        ps.setString(1, username);
        ps.setString(2, password);
        try (final ResultSet rs = ps.executeQuery()) {
            if (!rs.next()) {
                return "Invalid";
            }
            return rs.getString("Restriction");
        }
    }
}

You would use it thus:

public void checkLogin() {
    try {
        final String loginResult = checkLogin(con, username, password);
        switch (loginResult) {
            case "Admin":
                this.setVisible(false);
                MainMenuAdmin admin = new MainMenuAdmin();
                admin.setVisible(true);
                return;
            case "Engineer":
                this.setVisible(false);
                MainMenuEngineer engineer = new MainMenuEngineer();
                engineer.setVisible(true);
            default:
                JOptionPane.showMessageDialog(null, "Login Failed");
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}

Upvotes: 1

Keerthivasan
Keerthivasan

Reputation: 12880

Your approach is very much flawed. There is no point in fixing the error in a bad approach. Please change your query like this

SELECT Restriction,ADD_REQD_FIELDS..., FROM Users Login = ? and Password = ?

If the resultset count is 1, then you have a valid user.This way you can check the authentication of the user and retrieve the user information from the database as well. Your approach is unnecessarily over complicated.

Upvotes: 1

Related Questions