number of parameters, which is 2)."更新表时-6ren"> number of parameters, which is 2)."更新表时-我正在为大学做的期末项目需要帮助,我正在做一个图书馆类系统。我是 mySQL 的新手。 I'm having a problem on code saying, "Parameter index ou-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:48:36 24 4
gpt4 key购买 nike

我正在为大学做的期末项目需要帮助,我正在做一个图书馆类系统。我是 mySQL 的新手。

I'm having a problem on code saying, "Parameter index out of range (3 > number of parameters, which is 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 的变量。它必须是 SQL String 的一部分:

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

关于java - "Parameter index out of range (3 > number of parameters, which is 2)."更新表时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57509380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com