Update Statement在JSP中不起作用

时间:2013-07-02 07:52:57

标签: java database jsp java-ee

我是JSP页面和servlet的新手,我在servlet中有一个更新语句。这是:

PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
        "UPDATE users set username=?,password=?,name=?,surname=?,phone=?,address," +
        "email=? where username=?");
        checkDB.setString(8,request.getParameter("tUserName"));
        checkDB.setString(1,request.getParameter("tUserName"));
        checkDB.setString(2,request.getParameter("tPassword"));
        checkDB.setString(3,request.getParameter("tName"));
        checkDB.setString(4,request.getParameter("tSurName"));
        checkDB.setString(5,request.getParameter("tPhone"));
        checkDB.setString(6,request.getParameter("tAddress"));
        checkDB.setString(7,request.getParameter("tEmail"));
        result= checkDB.executeUpdate();

当我执行它时,有一个错误说java.sql.SQLException:参数索引超出范围(8>参数个数,即7)。我该如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:3)

检查PreparedStatement

PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
    "UPDATE users set username=?,password=?,name=?,surname=?,phone=?,address," +
    "email=? where username=?");

? sql中有7个PreparedStatement个占位符,你设置了8个参数。它应该是:

PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
    "UPDATE users set username=?,password=?,name=?,surname=?,phone=?,"+
    "address=?," + //You missed =?, here
    "email=? where username=?");

您可能错过了将=?,放在address之后。

注意: JSP用于查看。禁止在其中编写业务逻辑。还应避免使用scriptlet。让Controller或Servlet做业务逻辑。