Okay so what I want to do is when you select the row, (Image below) it querys the row for the data in column 'itemTag'. This is a unique reference to that item and so I know it will only ever delete the correct item from the database.
Image: Tale Columns
As you can see, there is a column named itemtag. There is a remove button, code below:
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();
}
}
Here is the code for the 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"));
}
Here is the code which gets all the items just in case you need that:
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;
}
}
Any ideas how I can get it to delete the selected row data from the mySql database. I get no errors with this, it deletes the row from the tableView but not from Sql. I presume I am doing something wrong with the String selectedItem = itemTagCol.getText();
line.
You need to get itemTag
.
addStockMaster asm = (addStockMaster )tvMainTable.getSelectionModel().getSelectedItem();
String tempItemTag = asm.getItemTag();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.