我正在尝试设置一个登录系统,当用户输入用户名和密码并按下登录按钮时,他的用户名和密码将在SQL语句中发送,以便与数据库中的用户匹配。如果用户名和密码正确(如果它们在数据库中匹配),则会创建一个User对象,其中包含来自数据库的所有用户数据,并且用户将被带到Admin Dashboard
在对结果集等进行了大量研究并经历了大量代码之后,我仍然无法使其在我的系统中运行。谁能告诉我一个很好的方法呢?或者告诉我这段代码是否有明显的错误?
当我输入用户名和密码并按登录时,我在控制台中收到错误:
java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5321)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5309)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:280)
at database.UserOperations.validateLog(UserOperations.java:45)
at gui.Login$2.mouseClicked(Login.java:77)
一些gui代码
JButton btnLogin = new JButton("Login");
btnLogin.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
UserOperations userOperations = new UserOperations();
ResultSet rset = userOperations.validateLog(textFieldUsername.getText(), textFieldPassword.getText());
User user = null;
try {
if (rset.next()) {
user = new User(rset.getInt("userID"), rset.getString("userName"), rset.getString("userPassword"), rset.getString("securityQuestion"), rset.getString("securityAnswer"),
rset.getInt("adminRights"), rset.getString("email"), rset.getString("fName"), rset.getString("phoneNumber"), rset.getString("department"));
dispose();
AdminDashboard admin = new AdminDashboard();
admin.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null, "Login fail!", "Error", JOptionPane.WARNING_MESSAGE);
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
});
validateLogin()方法
public ResultSet validateLog(String username, String userpassword)
{
try {
String sql = "SELECT userid, username, userpassword, securityquestion, securityanswer, adminrights, email, fname, phonenumber, department FROM systeuser WHERE username = ? AND userpassword = ?";
stmt = conn.prepareStatement(sql);
stmt.setString (2, username);
stmt.setString (3, userpassword);
rset = stmt.executeQuery ( );
}
catch(Exception e){
e.printStackTrace ( );
JOptionPane.showMessageDialog (null, "Login Failed", null, JOptionPane.WARNING_MESSAGE);
}
return rset;
}
答案 0 :(得分:2)
尝试小写:
user = new User(rset.getInt("userid"), rset.getString("username"),
rset.getString("userpassword"), rset.getString("securityquestion"),
rset.getString("securityanswer"), rset.getInt("adminrights"),
rset.getString("email"), rset.getString("fname"),
rset.getString("phonenumber"), rset.getString("department"));
而不是混合案例:
user = new User(rset.getInt("userID"), rset.getString("userName"),
rset.getString("userPassword"), rset.getString("securityQuestion"),
rset.getString("securityAnswer"), rset.getInt("adminRights"),
rset.getString("email"), rset.getString("fName"),
rset.getString("phoneNumber"), rset.getString("department"));
另外,改变这个:
stmt = conn.prepareStatement(sql);
stmt.setString (2, username);
stmt.setString (3, userpassword);
对此:
stmt = conn.prepareStatement(sql);
stmt.setString (1, username);
stmt.setString (2, userpassword);
答案 1 :(得分:0)
systeuser
这是你桌子的正确名称吗?