现在,我正在尝试使用javafx实现可编辑的Tableview,但我遇到了一个问题。到目前为止,我可以双击一个单元格并在其中写入新的值。但是问题是我无法更新数据库
用例是我可以在表单中添加路由,并且必须能够在添加时编辑这些路由
到目前为止,这里是我尝试过的事情:
要修改表格的代码
public void editTable() throws SQLException {
// This is in order to fill the box with the db values. But it is empty
ObservableList<String> planes = FXCollections.observableArrayList();
ObservableList<String> startFlughafen = FXCollections.observableArrayList();
ObservableList<String> zielFlughafen = FXCollections.observableArrayList();
Statement stateMentAirplane = connection.createStatement();
ResultSet myRsAirplane = stateMentAirplane.executeQuery(
"select * from gesellschaftflugzeuge where fluggesellschaft =\"" + fluggesellschaft + "\"");
while (myRsAirplane.next()) {
planes.add(myRsAirplane.getString("Hersteller"));
}
startAirport.setCellFactory(ComboBoxTableCell.forTableColumn());
airplane.setCellFactory(ComboBoxTableCell.forTableColumn());
}
public void prepareUpdateTable( String startFlughafen
) throws SQLException {
PreparedStatement pStatement = connection
.prepareStatement("Update fluglinie set startFlughafen =? ");
pStatement.setString(1, startFlughafen
);
}
public void initiateUpdateTable() {
try {
prepareUpdateTable(startAirport.getCellFactory().toString());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
初始化表
public void initializeTable() {
try {
// Ablesen
Statement pStatement = connection.createStatement();
flightList = FXCollections.observableArrayList();
ResultSet myRs = pStatement
.executeQuery("select * from fluglinie where fluggesellschaft =\"" + fluggesellschaft + "\"");
while (myRs.next()) {
flightList.add(new flightRouteAddModel(myRs.getString("startFlughafen"),
myRs.getString("zielFlughafen"), myRs.getString("startDatum"), myRs.getString("flugzeug"),
myRs.getInt("intervall"),
}
} catch (Exception e) {
System.out.println(e);
}
startAirport.setCellValueFactory(new PropertyValueFactory<>("startAirport"));
targetAirport.setCellValueFactory(new PropertyValueFactory<>("targetAirport"));
flightDate.setCellValueFactory(new PropertyValueFactory<>("flightDate"));
airplane.setCellValueFactory(new PropertyValueFactory<>("airPlane"));
table.setEditable(true);
table.setItems(flightList);
initiateUpdateTable();
try {
editTable();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我感谢你提供的所有答案