从mySql数据库中删除了选中的tableView行

时间:2017-11-07 19:45:38

标签: mysql javafx fxml

好的,我想要做的就是当你选择行时(下图),它会在列' itemTag'中查询数据行。这是对该项目的唯一引用,因此我知道它只会从数据库中删除正确的项目。

图像: Tale Columns

如您所见,有一个名为itemtag的列。有一个删除按钮,代码如下:

public void deleteItem() throws SQLException{
    int selectedIndex = tableView1.getSelectionModel().getSelectedIndex();
    String selectedItem = itemTagCol.getText();
    if(selectedIndex >= 0){
        con = handler.getConnection();
        String query1 = "DELETE FROM stock WHERE itemtag = ?";          
        pst = con.prepareStatement(query1);              
        pst.setString(1, selectedItem);
        pst.execute();       
        tableView1.getItems().remove(selectedIndex);
    } else {
        Alert alert = new Alert(AlertType.ERROR);
        alert.setTitle("ERROR:");
        alert.setHeaderText("No selection was made.");
        alert.setContentText("You have not selected an item to delete. Please try again.");
        alert.showAndWait();
    }
}

以下是tableView的代码:

private TableView<addStockMaster> tableView1;    
@FXML private TableColumn<?,?>itemIdCol;
@FXML private TableColumn<?,?> makeCol;
@FXML private TableColumn<?,?> modelCol;
@FXML private TableColumn<?,?> refCol;
@FXML private TableColumn<?,?> itemTagCol;

private ObservableList<addStockMaster> data;

private Connection con;
private PreparedStatement pst;
private DbHandlers handler;

@Override
public void initialize(URL url, ResourceBundle rb) {
    handler = new DbHandlers();
    tableView1.setEditable(true);
   data = FXCollections.observableArrayList();
   tableData();
   loadData();
}

public void tableData(){
    itemIdCol.setCellValueFactory(new PropertyValueFactory<>("itemiD"));
    makeCol.setCellValueFactory(new PropertyValueFactory<>("itemName"));
    modelCol.setCellValueFactory(new PropertyValueFactory<>("itemModel"));
    refCol.setCellValueFactory(new PropertyValueFactory<>("itemRef"));
    itemTagCol.setCellValueFactory(new PropertyValueFactory<>("itemTag"));

}

以下是获取所有项目的代码,以防您需要:

public class addStockMaster {
private int itemiD;
private String itemName;
private String itemModel;
private String itemRef;
private String itemTag;

public addStockMaster(int itemiD, String itemName, String itemModel, String itemRef, String itemTag) {
    this.itemiD = itemiD;
    this.itemName = itemName;
    this.itemModel = itemModel;
    this.itemRef = itemRef;
    this.itemTag = itemTag;
}

public addStockMaster() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

/**
 * @return the itemiD
 */
public int getItemiD() {
    return itemiD;
}

/**
 * @param itemiD the itemiD to set
 */
public void setItemiD(int itemiD) {
    this.itemiD = itemiD;
}

/**
 * @return the itemName
 */
public String getItemName() {
    return itemName;
}

/**
 * @param itemName the itemName to set
 */
public void setItemName(String itemName) {
    this.itemName = itemName;
}

/**
 * @return the itemModel
 */
public String getItemModel() {
    return itemModel;
}

/**
 * @param itemModel the itemModel to set
 */
public void setItemModel(String itemModel) {
    this.itemModel = itemModel;
}

/**
 * @return the itemRef
 */
public String getItemRef() {
    return itemRef;
}

/**
 * @param itemRef the itemRef to set
 */
public void setItemRef(String itemRef) {
    this.itemRef = itemRef;
}

/**
 * @return the itemTag
 */
public String getItemTag() {
    return itemTag;
}

/**
 * @param itemTag the itemTag to set
 */
public void setItemTag(String itemTag) {
    this.itemTag = itemTag;
}
}

我有什么想法可以让它从mySql数据库中删除选定的行数据。我没有得到任何错误,它从tableView删除行但不从Sql删除。我认为我对String selectedItem = itemTagCol.getText();行做错了。

1 个答案:

答案 0 :(得分:0)

您需要获得itemTag

addStockMaster asm = (addStockMaster )tvMainTable.getSelectionModel().getSelectedItem();
String tempItemTag = asm.getItemTag();