Java 如何将数据从tableview保存到数据库?
我无法在“表视图”单元格中保存数据编辑。当我编辑这些数据时,我按enter键,但数据不会保存在我的数据库中。我不知道怎么做。 这是我的密码Java 如何将数据从tableview保存到数据库?,java,sql,javafx,Java,Sql,Javafx,我无法在“表视图”单元格中保存数据编辑。当我编辑这些数据时,我按enter键,但数据不会保存在我的数据库中。我不知道怎么做。 这是我的密码 public String getValueAt(TableView<Note> table, int column, int row){ return table.getColumns().get(column).getCellObservableValue(row).getValue().toString(); }
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();
}
}
公共字符串getValueAt(TableView表格、int列、int行){
返回表.getColumns().get(column.getCellObservableValue(row.getValue().toString();
}
@FXML
void updateData(ActionEvent事件)引发ClassNotFoundException、SQLException{
String sql=“updatenotes set note=”“+getValueAt(tableView,2,0)+”,coef=”“+getValueAt(tableView,3,0)+”,total=”“+getValueAt(tableView,4,0)+”,evaluation=“+getValueAt(tableView,5,0)+”其中noteMatri;
Connection con=FirstConnection.Connect();
语句pst=con.createStatement();
字符串e=getValueAt(tableView,0,0);
系统输出打印ln(e);
字符串f=getValueAt(tableView,1,0);
系统输出打印ln(f);
字符串a=getValueAt(tableView,2,0);
系统输出打印项次(a);
字符串b=getValueAt(tableView,3,0);
系统输出打印ln(b);
字符串c=getValueAt(tableView,4,0);
系统输出打印ln(c);
字符串d=getValueAt(tableView,5,0);
系统输出打印ln(d);
int i=pst.executeUpdate(sql);
如果(i==1){
警报警报=新警报(警报类型.信息);
警报。设置标题(“Mise� 日效应�e”);
alert.setHeaderText(“注册d”�L�";;
alert.showAndWait();
}
}
行成功受影响,但当我查看数据库时,数据未更新。请帮我找回我做错了什么 从文档:
一旦您的单元格处于编辑状态,接下来您可能最感兴趣的事情就是如何提交或取消正在进行的编辑。这是您作为电池工厂供应商的责任。单元实现将根据用户输入(例如,当用户按下键盘上的Enter或ESC键时)知道编辑何时结束。发生这种情况时,您有责任根据需要调用Cell.committedit(对象)或Cell.cancelEdit()
正如@jarlh所问,“您提交了吗?”您如何知道“行成功受影响”?是否提交?警报框显示已成功更新有多少行已更新?看起来很有希望。你承诺了吗?请顺便说一句,像您这样访问表值是非常不寻常的(通常仅用于高级使用),从表的items集合中获取行项并用其属性更新后端有什么错?基本上是的,但在大多数情况下,javafx附带的单元格实现(f.i.TextFieldTableCell)已经足够好了,因此,客户机代码只是客户机而不是单元实现者:)