我坚持了一段时间,但不确定如何解决此问题。问题是我的SQL查询没有从javaFX文本字段和密码字段获取输入(我正在构建登录窗口)。
如果我手动输入值而不是从文本字段获取值,则程序可以正常运行,否则当您按登录按钮时,什么也不会发生。该问题发生在以下几行中,当然没有错误消息:
preparedStatement.setString(1,txtUserName.getText());
preparedStatement.setString(2,txtPassword.getText());
这是完整的代码:
public class LoginWindow implements Initializable{
@FXML
private TextField txtUserName;
@FXML
private PasswordField txtPassword;
@FXML
private Button btnLogin;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
// Setting the login button.
@FXML
private void setBtnLogin(ActionEvent event) {
try {
connection = DBUtilities.getConnection();
String sqlQuery = "SELECT * FROM user_login_details WHERE User_Name = ? AND User_Password = ?";
preparedStatement = connection.prepareStatement(sqlQuery);
preparedStatement.setString(1,txtUserName.getText());
preparedStatement.setString(2,txtPassword.getText());
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
DBUtilities.showInforMsg("Logged in:", "You have logged in!");
} else {
DBUtilities.showErrorMsg("Error:", "Invalid username or password");
}
}catch (Exception exception) {
exception.printStackTrace();
}finally {
DBUtilities.closePreparedStatement(preparedStatement);
DBUtilities.closeResultSet(resultSet);
DBUtilities.closeConnection(connection);
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
btnLogin.setOnAction(this::setBtnLogin);
}
}
最佳答案
非常感谢你。我只是没有给密码字段提供任何ID。