Issue
I'm failing to save the data edit in table view cell. when i edit those data then i press enter but the data isn't save in my database. i don't know how to do that. that's my code
public String getValueAt(TableView<Note> table, int column, int row){
return table.getColumns().get(column).getCellObservableValue(row).getValue().toString();
}
@FXML
void updateData(ActionEvent event) throws ClassNotFoundException, SQLException {
String sql = "UPDATE NOTES set note = '"+getValueAt(tableView,2,0)+"', coef = '"+getValueAt(tableView,3,0)+"', total = '"+getValueAt(tableView,4,0)+"', appreciation = '"+getValueAt(tableView,5,0)+"' where noteMatri = '"+getValueAt(tableView,0,0)+"';" ;
Connection con = FirstConnection.Connect();
Statement pst = con.createStatement();
String e = getValueAt(tableView,0,0);
System.out.println(e);
String f = getValueAt(tableView,1,0);
System.out.println(f);
String a = getValueAt(tableView,2,0);
System.out.println(a);
String b = getValueAt(tableView,3,0);
System.out.println(b);
String c = getValueAt(tableView,4,0);
System.out.println(c);
String d = getValueAt(tableView,5,0);
System.out.println(d);
int i = pst.executeUpdate(sql);
if(i == 1){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Mise � jour effectu�e");
alert.setHeaderText("Enregistrement d'�l�ments");
alert.showAndWait();
}
}
The row is affected successfully, but when I look on my database the data wasn't updated. please help me to retrieve what i do wrong.
Solution
From the doc: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableView.html
Once your cell is in an editing state, the next thing you are most probably interested in is how to commit or cancel the editing that is taking place. This is your responsibility as the cell factory provider. Your cell implementation will know when the editing is over, based on the user input (e.g. when the user presses the Enter or ESC keys on their keyboard). When this happens, it is your responsibility to call Cell.commitEdit(Object) or Cell.cancelEdit(), as appropriate.
As @jarlh asked, "Do you commit?"
Answered By - Shivam Puri