我在数据库中有三种用户类型。 http://oi44.tinypic.com/2z8qflw.jpg多用户登录java(管理员,用户,教师)
而且我的继承人登录表单 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) {
}
}
}
});
你的if否则层次结构是不正确的,检查它 –
如果其他层次似乎很好 – Batty
这是一个危险的使用SQL的方式,因为任何人都可以轻松地反编译jar,获得所需的信息,然后做一个'DROP TABLE'或'DROP DATABASE'在你的数据库上,突然间每个用户/表/数据库都消失了。 – Vallentin