我在数据库中有三种用户类型。 http://oi44.tinypic.com/2z8qflw.jpg
这是我的登录表单 http://oi44.tinypic.com/20p5v04.jpg
当我选择 admin 作为用户类型时,从数据库中输入用户名和密码,就会出现管理表单。但是当我选择教师和学生,并从数据库中输入用户名和密码时,只有 JOptionpane 显示,这是无效的详细信息。
这是我的登录 jframe 代码:
JButton btnLogin = new JButton("Login");
btnLogin.setFont(new Font("Book Antiqua", Font.PLAIN, 18));
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String sql = "SELECT * FROM useRecords ";
try {
ps = conn.prepareStatement(sql);
rs=ps.executeQuery();
String user = usern.getText();
String pwd = new String (passw.getPassword());
String type =(String)typeUser.getSelectedItem();
while(rs.next()) {
String uname = rs.getString("username");
String pass = rs.getString("password");
if ((user.equals(uname)) && (pwd.equals(pass))) {
if (type.equals("Admin")) { // ... admin
dispose();
aCai aCai = new aCai();
aCai.setVisible(true);
aCai.setExtendedState(Frame.MAXIMIZED_BOTH);
} else if (type.equals("Teacher")) { // ... teacher
dispose();
tCai tCai = new tCai();
tCai.setVisible(true);
tCai.setExtendedState(Frame.MAXIMIZED_BOTH);
} else {
dispose();
sCai sCai = new sCai();
sCai.setVisible(true);
sCai.setExtendedState(Frame.MAXIMIZED_BOTH);
}
} else {
JOptionPane.showMessageDialog(null, "User name and password do"
+ " not match!","ALERT!",
JOptionPane.ERROR_MESSAGE);
break;
}
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try{
rs.close();
ps.close();
} catch(Exception e) {
}
}
}
});