更新表时"Parameter index out of range (3 > number of parameters, which is 2)."



我需要帮助来完成我为大学制作的最终项目,我正在制作一个图书馆式的系统。我是 mySQL 的新手。

我在代码上遇到问题,"参数索引超出范围(3>参数数,即 2("。

PreparedStatement book_edit = book_db.prepareStatement("update books set ?='?' where id=?");
book_edit.setString(1, column); //where column is to set the column of the table
book_edit.setString(2, input); //where input is what you want to change
book_edit.setInt(3, book_id); //the primary key in my database
int i = book_edit.executeUpdate();
if (i != 0) {
System.out.println("UPDATED!!");
}
else {
System.out.println("Failed to Add Data!");
}

列名不能是PreparedStatement的变量。它必须是 SQLString的一部分:

PreparedStatement book_edit = book_db.prepareStatement("update books set " + column + "= ? where id = ?");
book_edit.setString(1, input); //where input is what you want to change
book_edit.setInt(2, book_id); //the primary key in my database

相关内容

  • 没有找到相关文章

最新更新