I get the following error when I attempt to delete a row: java.sql.SQLException: Before start of result set. Can not call deleteRow()
What I want to do is add a delete button, which removes the selected row from the TableView. It works to delete it from the Table. Yet it doesn't delete the row in the database and I've struggled a bit with different approaches trying to solve how to do it...
The delete button:
Button removeButton1 = new Button("Remove");
removeButton1.setMaxWidth(150);
removeButton1.setOnAction(e -> {
try {
int selectedIndex = itemTable.getSelectionModel().getSelectedIndex();
itemTable.setEditable(true);
itemTable.getItems().remove(selectedIndex);
inventoryData.removeItem();
} catch (SQLException e1) {
e1.printStackTrace();
}
});
Remove item method (error):
public void removeItem() throws SQLException {
Statement stmt = null;
try {
stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = stmt.executeQuery("SELECT * FROM Items");
uprs.deleteRow();
uprs.first();
} catch (Exception e){
e.printStackTrace();
} finally {
if(stmt != null){
stmt.close();
}
}
}
The problem (I think) is with deleteRow() as I am not expressing a row for it to delete... Yet I am quite lost as to how to solve this problem... With TextField, for example, I can get the string/int values and it'll find it in the database for me... But how do I do so when the user simply clicks a row? I tried to look up previous questions on stackoverflow but alas my confusion was only excarbated...
Thanks in advance.
EDIT
private final IntegerProperty itemId = new SimpleIntegerProperty(this,"itemId");
public IntegerProperty itemIdProperty(){
return itemId;
}
public final int getItemId(){
return itemIdProperty().get();
}
public final void setItemId(int itemId){
itemIdProperty().set(itemId);
}