Java Login 3 times Attempt Message box appears continuously

1.8k views Asked by At

I am creating a login attempt that will block the user or exit the system if he put or typed a wrong user password.

Please help me why my message box keeps looping continuously?

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);
    }
}
1

There are 1 answers

0
Stephan On

It seems that you don't break here on success:

  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++;