Java登录循环身份验证问题

时间:2017-04-23 00:15:44

标签: java loops passwords

我的java登录验证码有问题。我希望它只允许3次尝试。每次尝试后,都必须重新打开登录GUI。它会这样做,但是然后JOptionPane的消息“你已经使用了3次尝试中的x”仍然会弹出并计算尝试次数,即使我没有被允许在登录中输入任何内容。

代码在csv文件中搜索用户名和密码信息并验证它们。使用正确的信息,登录不是问题。我的问题是具体的循环,而不是我的密码的实际验证。我知道密码/用户名验证确实有效。循环的逻辑给了我错误。

非常感谢任何帮助,谢谢!

        String username = usernameText.getText();
        char[] passwrd = passwordField.getPassword();
        String password = new String(passwrd);
        boolean blnFound = false;

        statement.executeQuery("SELECT username, password FROM users WHERE username='" + username + "' " +
               "AND password='" + password + "';");
        ResultSet rs = statement.getResultSet();
        blnFound = rs.first();

        System.out.println("blnFound. Closing log in window.\n");

        // close login window upon pressing enter
        dispose();

       int i = 0;

       while (i < 3) {
           if (blnFound) {

               if (username == "admin") {
                   //pass along value of username
                   //add admin class
               } else {
                   //regular user
                   //pass along value of username

                   newTicket nw = new newTicket();
                   nw.username(username);

                   java.awt.EventQueue.invokeLater(new Runnable() {
                       public void run() {
                           new user().setVisible(true);
                       }
                   });
               }      
           } else {  //incorrect password code
               i++;

               String message = "Username or password incorrect." 
                        + "\n\n" + "You have used " + i + " out of 3 tries.";
               System.out.println("Show Dialog.");
               JOptionPane.showMessageDialog(null, message);    
               System.out.println("Restart login.");

               java.awt.EventQueue.invokeLater(new Runnable() {
                      public void run() {
                      new logIn().setVisible(true);
                      }
               });
           }
       }

       if (i == 3) {
            String end = "You have attempted too many times.";
            JOptionPane.showMessageDialog(null,end);
            System.exit(0);
        }

        statement.close();
        connect.close();

    } catch (Exception e1) {
        System.out.println(e1.getMessage());
    }
  }                                             
 }

0 个答案:

没有答案