閱讀我堅持這一段時間,我不知道如何解決這個問題。問題是我的SQL查詢沒有得到來自javaFX textfield和passwordfield的輸入(我正在構建一個登錄窗口)。 如果我手動輸入值而不是從文本字段中獲取它們,則程序可以正常工作,否則在按下登錄按鈕時不會發生任何事情。出現該問題在下面幾行,當然沒有錯誤消息:MySQL的預處理語句不從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);
}
}
通過輸入數據手動而不是從一個文本字段retreiving它我的意思是: 了PreparedStatement.setString(1,「管理員」); preparedStatement.setString(2,「admin」); – cris22tian
你能用'txtUserName.getText()。修剪()'和'txtPassword.getText()。修剪()',如果它的工作原理試試? – developer
我試過了,它不起作用。感謝您的迴應。任何其他想法可能會導致它? – cris22tian