gpt4 book ai didi

java - 参数索引超出范围(1 > 参数个数,即0),如何避免?

转载 作者:行者123 更新时间:2023-12-01 20:51:59 25 4
gpt4 key购买 nike

Java Code

I have been trying to updated the selected row values but Im getting Parameter index out of bound exception. Any suggestions? the statement is correct, also can anyone explain in why it occurs?

public class Editbook extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String booktitle = request.getParameter("booktitle");
String author = request.getParameter("author");
String category = request.getParameter("category");
String pages = request.getParameter("pages");
String desc = request.getParameter("description");
String isbn = request.getParameter("isbn");

Connection con = Logincheck.getConnection();
PreparedStatement ps = con.prepareStatement("update books set title ='"+booktitle+"', author ='"+author+"', category ='"+category+"', pages ='"+pages+"', description ='"+desc+"' where isbn ='"+isbn+"'");

ps.setInt(1, Integer.parseInt(isbn));
ps.setString(2, booktitle);
ps.setString(3, author);
ps.setString(4, category);
ps.setInt(5, Integer.parseInt(pages));
ps.setString(6, desc);

int i = ps.executeUpdate();
out.println("updated");
System.out.println(i + "updated");
} catch (Exception e) {System.out.println(e);}

}

}

最佳答案

PreparedStatement中,您直接输入参数值,而不使用任何?。所以当你写

 ps.setInt(1, Integer.parseInt(isbn));

该语句将第一次出现的 ? 替换为指定值。但由于没有?,它会给出参数索引超出范围的异常。

关于java - 参数索引超出范围(1 > 参数个数,即0),如何避免?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43199702/

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