我是Java新手,我很难理解登录用户角色的工作原理。这个概念和所有..我写了下面的代码,但它一直直接捕捉错误..如果有人能让我通过,我将不胜感激。
public void actionPerformed(ActionEvent arg0) {
try {
String query = "Select * from userdetails where username=? and password=? and role=?";
PreparedStatement pst = conn.prepareStatement(query);
pst.setString(1, txtusername.getText());
pst.setString(2, passwordField.getText());
pst.setString(3, cmbselect.getSelectedItem().toString());
ResultSet rs = pst.executeQuery();
int count = 0;
while (rs.next()) {
count = count + 1;
}
String role = (cmbselect.getSelectedItem().toString());
if (role == "ADMIN") {
JOptionPane.showMessageDialog(null, "WELCOME ADMIN");
frame.dispose();
AdminHome admhm = new AdminHome();
admhm.setVisible(true);
} else if (role == "NURSE") {
JOptionPane.showMessageDialog(null, "WELCOME NURSE");
ManagePatient mp = new ManagePatient();
mp.setVisible(true);
} else if (role == "DOCTOR") {
JOptionPane.showMessageDialog(null, "WELCOME Doctor");
Main m = new Main();
m.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "Username and password is INVALID");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}