2013-12-16 63 views
0

我正在創建一個登錄嘗試,如果用戶輸入或輸入了錯誤的用戶密碼,將會阻止用戶或退出系統。Java登錄3次嘗試連續出現消息框

請幫助我爲什麼我的消息框不斷循環?

private void login() { 

    String stru = ""; 
    stru = TxtUserName.getText(); 

    String strp = ""; 
    strp = TxtPassword.getText(); 
    if (stru.isEmpty() == true) { 
     JOptionPane.showMessageDialog(null, "Enter User Name"); 
     return; 
    } 

    if (strp.isEmpty() == true) { 
     JOptionPane.showMessageDialog(null, "Enter Password"); 
     return; 
    } 

    try { 

     //get database connection details 
     Connect mc = new Connect(); 

     //open connection 
     Connection connection; 
     connection = DriverManager.getConnection(mc.StrUrl, mc.StrUid, mc.StrPwd); 
     String str = ""; 
     str = "select * from lib_user where user_name =? and user_password =?"; 
     PreparedStatement pst = connection.prepareStatement(str); 
     pst.setString(1, stru); 
     pst.setString(2, strp); 
     ResultSet rs; 
     rs = pst.executeQuery(); 
     int i = 0; 
     do { 
      if (rs.next()) { 
       Connect.StrUser = TxtUserName.getText(); 
       MainForm m = new MainForm(); 

       this.setVisible(false); 
       JOptionPane.showMessageDialog(null, "Welcome to BIR Library Management System", "Welcome", JOptionPane.INFORMATION_MESSAGE); 
       m.setVisible(true); 
      } else { 
       i++; 
//   TxtPassword.disable(); 
       //   TxtUserName.disable(); 

       JOptionPane.showMessageDialog(null, "User name or password are not correct." + i); 

       //   return; 
      } 

     } while (i < 3); 

     System.out.println("You screwed up."); 

    } catch (Exception e) { 

     { 
      JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE); 
     } 
     //   System.exit(1); 
    } 
} 
+0

你需要打破,如果成功登錄,否則while循環不斷持續。 – dARKpRINCE

回答

0

看來你不打破這裏的成功:

if (rs.next()) { 
      Connect.StrUser = TxtUserName.getText(); 
      MainForm m = new MainForm(); 

      this.setVisible(false); 
      JOptionPane.showMessageDialog(null, "Welcome to BIR Library Management System", "Welcome", JOptionPane.INFORMATION_MESSAGE); 
      m.setVisible(true); 

      // no break here... 
     } else { 
      i++;