Take the 2-minute tour ×

I am having problem updating my mysql in my web application.. I can't Update an Entry.

It successfully enters the servlet... but it doesn't update an ENTRY, I suspect it was about my QUERY...

it was like this:

String query = "UPDATE members SET Username = ?, Password = ?, FirstName = ?, "
            + "MiddleName = ?, LastName = ?, Address = ?, Email = ? "
            + "WHERE memberId = ? AND Username = ?";

is that valid?

This is my servlet code:

public class EditMember extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
            + "WHERE memberId = ? AND Username = ?";

    try {
        con = DriverManager.getConnection(Connect.CONNECTION_STRING,
                Connect.USERNAME, Connect.PASSWORD);

        String currUserame = request.getParameter("CreateUsername");
        String currPassword = request.getParameter("CreatePassword");
        String currFirstName = request.getParameter("CurrFirstName");
        String currMiddleName = request.getParameter("currMiddleName");
        String currLastName = request.getParameter("currLastName");
        String currAddress = request.getParameter("currAddress");
        String currEmail = request.getParameter("currEmail");

        int memberId = Integer.parseInt(request.getParameter("memberId"));
        String oldPassword = request.getParameter("Password");

            stmt = con.prepareStatement(query);
            stmt.setString(1, currUserame);
            stmt.setString(2, currPassword);
            stmt.setString(3, currFirstName);
            stmt.setString(4, currMiddleName);
            stmt.setString(5, currLastName);
            stmt.setString(6, currAddress);
            stmt.setString(7, currEmail);

            stmt.setInt(8, memberId);
            stmt.setString(9, oldPassword);

            out.println("Success");//for testing only

    } catch (Exception ex) {
    } finally {
        try {
        } catch (SQLException ex) {
            out.println("Sql Exception");

//HttpServlet methods were placed here


and this is my jsp:

    <form action="EditMember">
        <h1>Enter old Info</h1><br/>
        Enter your Id:
        <input type="text" name="memberId"><br/>
        Enter your Password:
        <input type="password" name="Password" /><br/>

        <h1>Enter New Info</h1><br/>
        User Name:
        <input type="text" name="CreateUsername" /><br/>
        Re-Enter/New Password:
        <input type="password" name="CreatePassword" /><br/>
        First Name:
        <input type="text" name="CurrFirstName"><br/>
        Middle Name:
        <input type="text" name="currMiddleName"><br/>
        Last Name:
        <input type="text" name="currLastName"><br/>
        <input type="text" name="currAddress"><br/>
        Email Add.:
        <input type="text" name="currEmail">
        <input type="submit" name="Submit" id="Submit" value="Save New Info" />

I already check each variable I used in JSP if it matched with the variable i used in my servlet... it was all the same.. So aside from the QUERY i don't know other things that causes the problem...

please help.... thanks a lot in advance!

share|improve this question
ooooppppsss.... a variable at my query conflicts the code below :) –  iamanapprentice Mar 15 '11 at 11:13
hello. musta na? Do you still have a problem with this? YM u? –  newbie Mar 15 '11 at 12:53
ok na newbie... thanks :) –  iamanapprentice Mar 15 '11 at 13:46
add comment

1 Answer

up vote 1 down vote accepted

You are binding the wrong variable to the last parameter on your query. You are doing stmt.setString(9, oldPassword); and it probably should be stmt.setString(9, oldUsername);.

And btw, if memberId is a primary or unique key, then you don't need the AND username = ? part in your WHERE clause.

share|improve this answer
thanks Sir Carlos... I saw it when I review my code... and Thanks about the "WHERE" clause i didnt knew that it was valid... Thanks for the new Information... Thanks a Lot Sir.. :) –  iamanapprentice Mar 15 '11 at 11:17
You are welcome :) –  Carlos Campderrós Mar 15 '11 at 12:13
add comment

Your Answer


