gpt4 book ai didi

java - MySQL记录不保存

转载 作者:行者123 更新时间:2023-11-29 19:06:42 27 4
gpt4 key购买 nike

我在将记录插入 mySQL 数据库时遇到问题,查询根本没有执行,我不明白为什么它在捕获异常时不打印任何错误。有什么想法吗?我已经多次检查了拼写和表格本身,该连接也有效,因为它在我的登录窗口中使用,该窗口出现在下面所示的窗口之前。

public class RegisterStudent implements Initializable{

@FXML
TextField txtID;
@FXML
TextField txtFirstName;
@FXML
TextField txtSecondName;
@FXML
TextField txtGender;
@FXML
TextField txtDOB;
@FXML
TextField txtAddress;
@FXML
TextField txtPostCode;
@FXML
TextField txtMobileNumber;
@FXML
Button btnRegister;
@FXML
Button btnClear;

Connection connection = null;
PreparedStatement preparedStatement = null;

// Set Clear Button.
@FXML
private void setBtnClear() {
txtID.clear();
txtFirstName.clear();
txtSecondName.clear();
txtGender.clear();
txtDOB.clear();
txtAddress.clear();
txtPostCode.clear();
txtMobileNumber.clear();
}
// Set Register Button - Saves student record to database.
@FXML
private void setBtnRegister(ActionEvent event) {
try {
if(txtFirstName.getText().trim().isEmpty() ||
txtSecondName.getText().trim().isEmpty() ||
txtGender.getText().trim().isEmpty() ||
txtDOB.getText().trim().isEmpty() || txtAddress.getText().trim().isEmpty()
|| txtPostCode.getText().trim().isEmpty() || txtMobileNumber.getText().trim().isEmpty()) {
DBUtilities.showErrorMsg("Error:", "All fields must be populated!");
}else {
connection = DBUtilities.getConnection();
String SQLQuery = "INSERT INTO student_details ('First_Name', 'Second_Name', 'Gender', 'Date_Of_Birth', 'Address', 'Post_Code', 'Mobile_Number')" + " VALUES (?, ?, ? ,? ,? ,? ,?)";
preparedStatement = connection.prepareStatement(SQLQuery);
preparedStatement.setString(1, txtFirstName.getText().trim());
preparedStatement.setString(2, txtSecondName.getText().trim());
preparedStatement.setString(3, txtGender.getText().trim());
preparedStatement.setString(4, txtDOB.getText().trim());
preparedStatement.setString(5, txtAddress.getText().trim());
preparedStatement.setString(6, txtPostCode.getText().trim());
preparedStatement.setString(7,
txtMobileNumber.getText().trim());
preparedStatement.executeUpdate();
DBUtilities.showInforMsg("Record Saved", "Record has been saved
successfully!");
}
}catch (Exception exception) {
//DBUtilities.showErrorMsg("Error:", "Record could not be saved!");
exception.printStackTrace();
}finally {
DBUtilities.closePreparedStatement(preparedStatement);
DBUtilities.closeConnection(connection);
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
btnRegister.setOnAction(this::setBtnRegister);
}
}

最佳答案

尝试在 preparedStatement.executeUpdate(); 此语句之后使用 connection.commit;。可能是自动提交是错误的。

关于java - MySQL记录不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43431782/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com