错误:未为参数1指定值

时间:2018-06-23 17:49:25

标签: java

该代码用于获取表视图中的选定行,并将选定行的状态设置为3(在我的数据库中表示保留)。

我的代码:

@FXML
private TableView tableView;

@FXML
public void handleReserveBtn(ActionEvent event) {
    TableView.TableViewSelectionModel selectionModel = tableView.getSelectionModel();
    ObservableList selectedCells = selectionModel.getSelectedCells();

    try {
        Connection conn = DB.getInstance().getConn();
        PreparedStatement stmt = conn.prepareStatement("UPDATE items SET status = 3 WHERE "+selectedCells+" = ?");
        stmt.executeUpdate();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

我得到的唯一错误是:没有为参数1指定值

请帮助我。

1 个答案:

答案 0 :(得分:0)

您必须设置什么?表示在PreparedStatement中。

尝试一下:

@FXML
private TableView tableView;

@FXML
public void handleReserveBtn(ActionEvent event) {
TableView.TableViewSelectionModel selectionModel = tableView.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();

try {
    Connection conn = DB.getInstance().getConn();
    PreparedStatement stmt = conn.prepareStatement("UPDATE items SET status = 3 WHERE 
"+selectedCells+" = ?");
   //use PreparedStatement.set[datatype] and pass in the question mark index (starting from 1, not 0) along with your specified value for it
   stmt.setString(1, "value");//if the datatype for selectedCells is char or varchar
   stmt.setInt(1, value);//if the datatype for selectedCells is int
    stmt.executeUpdate();
} catch (Exception e) {
    System.out.println(e.getMessage());//replace this with a Logger
}
}