【问题标题】:JavaFX error when attempting to delete row from database尝试从数据库中删除行时出现 JavaFX 错误
【发布时间】:2017-06-19 12:21:05
【问题描述】:

当我尝试删除一行时出现以下错误:java.sql.SQLException:在结果集开始之前。无法调用 deleteRow()

我想要做的是添加一个删除按钮,它从 TableView 中删除选定的行。它可以从表中删除它。然而它并没有删除数据库中的行,而且我在尝试解决如何做到这一点的不同方法上有点挣扎......

删除按钮:

 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();
        }
    });

删除项目方法(错误):

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();
        }
    }

}

问题(我认为)出在 deleteRow() 上,因为我没有表达要删除的行...但是对于如何解决这个问题我很迷茫...例如,对于 TextField,我可以获取字符串/整数值,它会为我在数据库中找到它...但是当用户只需单击一行时我该怎么做呢?我试图在 stackoverflow 上查找以前的问题,但可惜我的困惑只是加剧了......

提前致谢。

编辑

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);
}

【问题讨论】:

  • 据我所知,您必须使用 next() 遍历 ResultSet。
  • @OHGODSPIDERS 我试过这个,虽然它没有给出错误,但数据库没有任何变化。除非我也做错了......?
  • SELECT SQL 语句从数据库表中选择实体。要从表中删除,请使用 DELETE 语句。看,也许是stackoverflow.com/questions/27345614/how-to-delete-row-from-sql 或mysql documentation
  • @James_D 所以是这样的吗? “从项目中删除 item_id = ?” ... item_id 是主键...但是如何从用户选择行中获取 item_id?
  • 大概您将id设置为代表行中数据的类的属性,因此您可以从所选项目中获取它。这个论坛上没有人是千里眼:我们不知道你是如何设置你的模型类的。

标签: java mysql button javafx


【解决方案1】:

你可以的

removeButton1.setOnAction(e -> {
    try {
        Item selectedItem = itemTable.getSelectionModel().getSelectedItem();
        itemTable.getItems().remove(selectedItem);
        inventoryData.removeItem(selectedItem.getItemId());

    } catch (SQLException e1) {
        e1.printStackTrace();
    }
}

与

public void removeItem(int itemId) throws SQLException {
    PreparedStatement stmt = null;

    try {
        stmt = connection.prepareStatement("DELETE FROM Items where item_id = ?");
        stmt.setInt(1, itemId);
        stmt.execute();
    } catch (Exception e){
        e.printStackTrace();
    } finally {
        if(stmt != null){
            stmt.close();
        }
    }

}

(我假设您的 JavaFX 表的模型类是 Item,即您有一个 TableView<Item>:根据需要在第一个块中更改 selectedItem 的类型。)

【讨论】:

  • 这解决了它。干杯!还有一个问题:你能推荐一些关于这个的教程/链接吗?
  • 有很多:我只想用谷歌搜索“SQL 简介”。 W3schools 对于初学者来说是一个合理的起点。
猜你喜欢
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多