2016-08-21 123 views
0

我創建了兩列可編輯的TableView,用戶可以編輯並更改每個單元格內的數據。現在的問題是,一旦用戶在每個小區周圍改變了一些數據,我怎麼然後保存這些數據或打印出來的方式添加到SQL查詢像下面這個例子JavaFX將可編輯的TableView保存到SQL

INSERT INTO table_name 
VALUES (Value from table,Value from table,Value from table,...); 

//編輯單元

PriceColumn.setCellFactory(TextFieldTableCell.forTableColumn()); 
PriceColumn.setOnEditCommit(
     new EventHandler<CellEditEvent<NewCustomerList, String>>() { 
      @Override 
      public void handle(CellEditEvent<NewCustomerList, String> t) { 
       ((NewCustomerList) t.getTableView().getItems().get(
         t.getTablePosition().getRow())).setPrice(t.getNewValue()); 
      } 
     } 
); 

回答

1

你可以通過獲取所需的值,然後將它們傳遞到DAO類來執行對DB查詢做到這一點。舉例如下 -

PriceColumn.setOnEditCommit(
     new EventHandler<CellEditEvent<NewCustomerList, String>>() { 
      @Override 
      public void handle(CellEditEvent<NewCustomerList, String> t) { 
       ((NewCustomerList) t.getTableView().getItems().get(t.getTablePosition().getRow())).setPrice(t.getNewValue()); 
       String newPrice = t.getNewValue(); 
       String uniqueIdentifier = t.getRowValue().getUniqueIdentifier(); //Unique identfier is something that uniquely identify the row. It could be the name of the object that we are pricing here. 
       daoObj.updatePrice(newPrice, uniqueIdentifier); //Call DAO now 
      } 
     } 
); 

而且在DAO類的深暗色的叢林某處,

private final String updateQuery = "UPDATE <TABLE_NAME> SET <PRICE_COLUMN> = ? WHERE <UNIQUE_COLUMN> = ?"; //If you require multiple columns to get a unique row, add them in the where clause as well. 

public void updatePrice(String newPrice, String uniqueIdentifier) { 
    PreparedStatement ps = con.prepareStatement(updateQuery); //con is the connection object 
    ps.setString(1,uniqIdentifier); //if a string 
    ps.setString(2,newPrice); //if a string 
    ps.executeQuery(); 
} 

如果這不是您所期望的,那麼可以請你澄清你的要求嗎?

相關問題