Search code examples
javasql-serverswingjoptionpane

JOptionPane.showMessageDialog won't run and the next line is also not running


At the first try before i update the code and updating the database all system running correctly. The problem is when username and password correct the message box wont appear the next form too. The massage box just appear when username and password wrong. The database is already connected and the program read it.

Here is my code:

private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
    try
    {
        pr = con.prepareStatement("SELECT * FROM Employee WHERE username = ? and pass = ?");

        String Us = Username.getText();
        String PW = Password.getText();

        if(Username.equals("") || Password.equals(""))
        {
            JOptionPane.showMessageDialog(null, "You Need To Fill Username and Password");
        }
        else
        {
            pr.setString(1, Us);
            pr.setString(2, PW);

            rs = pr.executeQuery();
            rs.next();

            String EmployeeName = rs.getString("nama");
            String Position = rs.getString("posisi");
            String EmpID = rs.getString("id");
            EmpLem = EmpID;

            if (Username.equals(rs.getString("username"))&& Password.equals(rs.getString("pass")))
            {

                if (Position.equals("Service"))
                {
                    JOptionPane.showMessageDialog(null, "Login Success! Welcome " + EmployeeName + ".");
                    EmployeeMenu serviced = new EmployeeMenu();
                    serviced.show();
                    this.hide();
                }

                else if (Position.equals("superadmin"))
                {
                    JOptionPane.showMessageDialog(null, "Login Success! Welcome " + EmployeeName + ".");
                    SuperMenu sm = new SuperMenu();
                    sm.show();  
                    this.hide();

                }

                else if (Position.equals("Warehouse"))
                {
                    JOptionPane.showMessageDialog(null, "Login Success! Welcome " + EmployeeName + ".");
                    WareHouseMan sm = new WareHouseMan();
                    sm.show();
                    this.hide();

                }
            }
        }

    }    

    catch(Exception ex)
    {
        JOptionPane.showMessageDialog(null, "Username or Password Didn't Match!!");
    }
}  

Here :

if (Position.equals("Service"))
{
    JOptionPane.showMessageDialog(null, "Login Success! Welcome " + EmployeeName + ".");
    EmployeeMenu serviced = new EmployeeMenu();
    serviced.show();
    this.hide();
}

Solution

  •  if(Username.equals("") || Password.equals(""))
    

    In this condition, if your username will not be null and your password will be null then this would return true because

    OR operator checks the first condition, if that's true then it will not check the second one. If the first one is not true then it will check the other condition

    So first correct that.

    Second, in place of rs.next(); try to use the following, maybe it works.

    while(rs.hasNext()){......}