我试图在用户验证为“管理员”或“用户”时调用页面,但似乎有问题。
private void validateLogin() {
if (getFieldData() == true) {
username = txtname.getText();
String password = new String(txtpassword.getPassword());
Object type = cbType.getSelectedItem();
//validate login and password here.
//validity will be done by sending login/password to the database
String sql = "select count(*) from usermanagement where UserName='"
+ username + "' and " + "Password='" + password + "'";
ResultSet rs = datacon.queryTable(sql);
try {
rs.next();
if (rs.getInt(1) > 0) {
if (type.equals("Admin")) {
MainMenu mainmenu = new MainMenu();
this.dispose();
mainmenu.setVisible(true);
} else {
UserMenu usermenu = new UserMenu();
this.dispose();
usermenu.setVisible(true);
}
} else {
JOptionPane.showMessageDialog(this,
"Incorrect username or password or user category",
"Error", JOptionPane.ERROR_MESSAGE);
clearField();
txtname.requestFocusInWindow();
cbType.setSelectedIndex(0);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
我在这里看不到您的输入,用户名和密码。最好附上您的日志,以便瞥见Exception对您的问题的陈述。
从我在代码中看到的是我的建议,
你应该避免在你的sqls上使用这个UserName='"+username +"'
,这对于特殊的Charaters来说可能会破坏你的sql。我建议您遵守PreparedStatement http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
最好坚持if(rs.next())
检查记录是否存在。访问rs.getInt(1)
是不安全的,如果您没有任何匹配记录,可能会破坏您的申请。