I have a problem with inserting a record into mySQL database, the query simply does not get executed and I don't understand why it does not print any errors when catching an exception. Any ideas please? I have checked the spelling many times and the table itself, the connection also works because it is used in my Login Window which appears before the one shown below.
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);
}
}