mardi 24 septembre 2019

My nested IF condition in JAVA doesn't work?

I'm checked my code for many time but I don't know why if I input as employee. Result is jump to else. Please help me solve problem and tell me why my code not working?

     private void btnSignIn_actionPerformed(ActionEvent e) {
                String username = textFieldNumber.getText(); //variable get username from input
                String password = new String(passwordFieldPassword.getPassword());//variable get password from input
                JFrameMain jFrameMain = new JFrameMain(); // create instance
                try {
                    //Connect Database
                    Connection conn = ConnectDB.getConnection();
                    PreparedStatement preparedStatement = conn.prepareStatement("select * from user"); //SQL query
                    ResultSet resultSet = preparedStatement.executeQuery();
                    // 1st while loop start
                    while (attempts != 0 && resultSet.next()) {
                        String fullname_db = resultSet.getString("fullname"); //fullname from db
                        String username_db = resultSet.getString("username"); //username from db
                        String password_db = resultSet.getString("password"); //password from db
                        int idrole_db = resultSet.getInt("id_role"); //id of role from db  
                        // 2nd while loop start
                        while (attempts != 0) {
                            if (username.equals(username_db) && password.equals(password_db)) {
                                if(idrole_db == 1) {
                                    jFrameMain.setVisible(true);
                                    setVisible(false);
                                    jFrameMain.AssignMenu("admin");
                                    JOptionPane.showMessageDialog(null, "You have logged in with admin account, " + fullname_db,"WELCOME BACK", JOptionPane.INFORMATION_MESSAGE);
                                    break;
                                // Log in as admin
                                }else if(idrole_db == 2) {
                                    jFrameMain.setVisible(true);
                                    setVisible(false);
                                    jFrameMain.AssignMenu("employee");
                                    JOptionPane.showMessageDialog(null, "You have logged in with employee account", "WELCOME BACK, " + fullname_db,JOptionPane.INFORMATION_MESSAGE);
                                    break;
                                // Log in as employee
                                }
                                } else {
                                       attempts--;
                                       lblNewLabel.setText("Account Invalid, You have " + attempts + " try left.");
                                        if(attempts == 0) {
                                        JOptionPane.showMessageDialog(null,
                                            "Please contact your manage to create account or reset password", "LOG IN FAILED MANY", JOptionPane.ERROR_MESSAGE);
                                    System.exit(0);
                                // show error if input wrong
                                }
                                return;
                            }
                        }
                    }
                } catch (Exception e2) {
                    System.out.println(e2.getMessage());
                  //catch exception
                }
            }

Aucun commentaire:

Enregistrer un commentaire