Reputation: 45
public void saveEmployeeButtonPushed(ActionEvent event) {
if (validPasswords()) {
try {
if (employee != null) {
updateEmployee();
employee.updateEmployeeInDB();
} else {
if (imageChanged) {
employee = new Employee(firstNameTextField.getText(),
lastNameTextField.getText(), eType.getValue().toString(),
phoneNumberTextField.getText(), Integer.parseInt(salaryTextField.getText().toString()),
birthday.getValue(), imageFile, pwField.getText());
} else {
if (Validation.emptyCheck(firstNameTextField.getText()) == false) {
JOptionPane.showMessageDialog(null, "Please enter the first name");
} else {
employee = new Employee(
firstNameTextField.getText(), lastNameTextField.getText(),
eType.getValue().toString(), phoneNumberTextField.getText(),
Integer.parseInt(salaryTextField.getText().toString()), birthday.getValue(),
pwField.getText());
}
}
errMsgLabel.setText("");
employee.insertIntoDB();
}
SceneChanger sc = new SceneChanger();
sc.changeScene(event, "Employee.fxml", "Employee");
} catch (Exception e) {
errMsgLabel.setText(e.getMessage());
}
}
}
This is the method for save
public void updateEmployeeInDB() throws SQLException {
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bakery", "root", "a3756421");
String sql = "UPDATE employee SET eName1=?,eName2=?,eType=?,phoneNumber=?,eSalary=?,birthday=?,imageFile=?" + "WHERE eId = ?";
preparedStatement = conn.prepareStatement(sql);
Date db = Date.valueOf(birthday);
preparedStatement.setInt(1, eId);
preparedStatement.setString(2, eName1);
preparedStatement.setString(3, eName2);
preparedStatement.setString(4, eType);
preparedStatement.setString(5, phoneNumber);
preparedStatement.setInt(6, eSalary);
preparedStatement.setDate(7, db);
preparedStatement.setString(8, imageFile.getName());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
if (conn != null)
conn.close();
if (preparedStatement != null)
preparedStatement.close();
}
}
This is the method for update employee
CREATE TABLE employee
(
eId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
eName1 VARCHAR(30),
eName2 VARCHAR(30),
eType VARCHAR(30),
phoneNumber VARCHAR(12),
eSalary INT,
birthday DATE,
imageFile VARCHAR(100)
);
This is the table I'm using in the database
Even when I press the save button, not only it doesn't update into the database but also it doesn't change the scene. I have checked whether the save button has a problem with the scene-builder but it was all fine.
Upvotes: 1
Views: 31
Reputation: 805
Your preparedStatement has wrong order. You are setting id first but in query It is last. I also changed sql query as there is no need for +
String sql = "UPDATE employee SET eName1=?,eName2=?,eType=?,phoneNumber=?,eSalary=?,birthday=?,imageFile=? WHERE eId = ?";
preparedStatement = conn.prepareStatement(sql);
Date db = Date.valueOf(birthday);
preparedStatement.setString(1, eName1);
preparedStatement.setString(2, eName2);
preparedStatement.setString(3, eType);
preparedStatement.setString(4, phoneNumber);
preparedStatement.setInt(5, eSalary);
preparedStatement.setDate(6, db);
preparedStatement.setString(7, imageFile.getName());
preparedStatement.setInt(8, eId); // THIS IS LAST NOW
Upvotes: 1