user2110714
user2110714

Reputation:

Update Statement does not work in JSP

I am new to JSP pages and servlets andI have an update statement in a servlet. Here it is:

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();

When i execute it, there is an error saying that java.sql.SQLException: Parameter index out of range (8 > number of parameters, which is 7). How can i fix this?

Thanks

Upvotes: 0

Views: 98

Answers (1)

AllTooSir
AllTooSir

Reputation: 49372

Check your PreparedStatement:

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

There are 7 ? placeholders in the PreparedStatement sql and you are setting 8 parameters.It should probably be :

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

You probably missed to put =?, after address.

Note: JSPs are for view. Prohibit from writing business logic in it . Use of scriptlets should also be avoided . Have a Controller or Servlet to do business logic.

Upvotes: 3

Related Questions