private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String query;
boolean login = false;
String username = jTextField1.getText();
String password = jTextField2.getText();
try{
query = "SELECT (cUsername AND cPassword) FROM Customer WHERE cUsername = '"+username+"' AND cPassword = '"+password+"'";
pst = conn.prepareStatement(query);
pst.setString(1, username);
pst.setString(2, password);
pst.executeQuery();
String userCheck = rs.getString(1);
String passCheck = rs.getString(2);
if((userCheck.equals(username)) && (passCheck.equals(password)))
{
login = true;
System.out.println("It actually works?!");
}
else
{
login = false;
System.out.println("Psyche, that's the wrong number!");
}
}
catch(Exception e){
System.out.println(e);
}
System.exit(0);
}
我目前在代码中实现登录系统时遇到了困难。我正在尝试从用户名和密码jTextFields中检索文本,然后将它们查询到数据库,但它无法正常工作。目前我正在
java.lang.ArrayIndexOutOfBoundsException: 0
我很遗憾不知道为什么。任何帮助都会非常感激。
答案 0 :(得分:2)
您的查询和背后的逻辑需要做很多工作:
首先,如果你想要一个参数化的查询,你必须使用'?'而不是价值。然后pst.setString就可以了。
其次,您必须影响pst.executeQuery();
到ResultSet
query = "SELECT cUsername, cPassword FROM Customer WHERE cUsername = ? AND cPassword = ?";
pst = conn.prepareStatement(query);
pst.setString(1, username);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
String userCheck = rs.getString(1);
String passCheck = rs.getString(2);
}