-2

我正在处理用户登录页面。我希望管理员可以访问管理员部分,但不能访问普通用户。以下代码有效,但如果管理员以外的用户登录,他们会收到错误的密码错误并被发送到正确的页面。如果允许否定答案和肯定答案,我认为嵌套有问题。对于此事,我将不胜感激。

        try {
        String sql="Select * from Users1";
        String host ="jdbc:derby://localhost:1527/Nash";
        String uName = "CON";
        String uPass = "smokes";
        Connection con = DriverManager.getConnection(host, uName, uPass);
        Statement stmt=con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        String user= userName.getText();
        String pwd= password.getText();
        while(rs.next()) {
        String uname=rs.getString("User_name");
        String pass=rs.getString("Password");
        String admin1=rs.getString("admin");

if ((user.equals(uname)) && (pwd.equals(pass)))
    { 
    mainPanel.setVisible(true);
    blankContent.setVisible(true);
    adminButton.setEnabled(false);
    receiptContent.setVisible(false);
    memberContent.setVisible(false);
    securityPanel.setVisible(false);
    }

        if ((user.equals(uname)) && (pwd.equals(pass))&&(admin1.equals("y")))
        {
            mainPanel.setVisible(true);
            blankContent.setVisible(true);
            adminButton.setEnabled(true);
            receiptContent.setVisible(false);
            memberContent.setVisible(false);
            securityPanel.setVisible(false);
         }
else 
    {
            JOptionPane.showMessageDialog(null,  "User name and password do"
                    + " not match!","ALERT!", JOptionPane.ERROR_MESSAGE); 

            }   
}   } catch (SQLException ex) {
        Logger.getLogger(program.class.getName()).log(Level.SEVERE, null, ex);
    }

EDIT 下面是编辑后的代码链接---> 新的修改后的代码链接

4

3 回答 3

3

您需要将您else的水平移出一层。代码格式很难判断发生了什么,但 else 在 adminif语句中。像这样:

if ((user.equals(uname)) && (pwd.equals(pass)))
{ 
    if (admin1.equals("y"))
    {
        // ... admin
    }
    else
    {   
        // ... regular user
    }
}
else 
{
    JOptionPane.showMessageDialog(null,  "User name and password do"
           + " not match!","ALERT!", JOptionPane.ERROR_MESSAGE); 
}
于 2012-12-31T20:04:02.727 回答
0

做喜欢

if ((user.equals(uname)) && (pwd.equals(pass)))
    { 
    mainPanel.setVisible(true);
    blankContent.setVisible(true);
    receiptContent.setVisible(false);
    memberContent.setVisible(false);
    securityPanel.setVisible(false);
    adminButton.setEnabled(false);
          if(admin1.equals("y"))
                 adminButton.setEnabled(true);

    }


else 
    {
            JOptionPane.showMessageDialog(null,  "User name and password do"
                    + " not match!","ALERT!", JOptionPane.ERROR_MESSAGE); 

            }  
于 2012-12-31T20:21:50.663 回答
0

你做的一切都是正确的,除了你错过了一个导致这个问题的小故障(From your pastebin code in your comment)

您的 while 语句在这里是个大问题。它将继续检查数据库中的所有记录。

break获得所需用户后使用。

boolean isPass = false;
while(rs.next()){
    // Your user authentication 
    if ((user.equals(uname)) && (pwd.equals(pass)))
    { 
        if (admin1.equals("y")) {
            // do your admin operation
            break;
        } else {   
            // do your non admin operation
            break;
        }
    } else {
        // if authentication fails 
        isPass = true
    }
}
// Outside of while block
if(isPass){
    // if authentication fails show error message
    JOptionPane.showMessageDialog(null,  "User name and password do"
           + " not match!","ALERT!", JOptionPane.ERROR_MESSAGE); 
}

我展示了这个代码片段来展示如何使用 break 并注意你也应该这样做breakelse part否则它将继续为所有那些在身份验证中变为错误的记录显示错误消息if

我会建议从另一种方法获取您的数据库值,并用另一种方法进行身份验证。

于 2012-12-31T21:27:13.037 回答