Java代碼參數索引超出範圍(1>參數個數,即0),如何避免?
我一直在嘗試更新選定的行值,但我得到的參數索引超出了約束的異常。有什麼建議麼?聲明是正確的,任何人都可以解釋它爲什麼會發生?
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);}
}
}
您正在混合連接和SQL參數。 –