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