JOptionPane.showMessageDialog将不会运行,并且下一行也不会运行

时间:2018-06-30 16:43:43

标签: java sql-server swing joptionpane

在我第一次尝试更新代码并更新数据库之前,所有系统都正确运行。问题是当用户名和密码正确时,消息框也不会出现在下一个表单中。当用户名和密码错误时,将显示按摩框。数据库已连接,程序已读取它。

这是我的代码:

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!!");
    }
}  

这里:

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

1 个答案:

答案 0 :(得分:0)

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

在这种情况下,如果您的用户名不为null,密码为null,则返回true,因为

OR 运算符将检查第一个条件,如果是,那么它将不检查第二个条件。如果第一个条件不正确,它将检查其他条件

所以首先要纠正它。

第二,代替rs.next();尝试使用以下内容,也许可行。

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