开发者

Update mysql in Web app

开发者 https://www.devze.com 2023-02-16 18:24 出处:网络
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...

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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String query = "UPDATE members SET Username = ?, Password = ?, FirstName = ?, "
            + "MiddleName = ?, LastName = ?, Address = ?, Email = ? "
            + "WHERE memberId = ? AND Username = ?";

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

        String currUserame = request.getParameter("CreateUsername");
        String currPassword = request.getParameter("CreatePassword");
   开发者_StackOverflow中文版     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);
            stmt.executeUpdate();

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

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

//HttpServlet methods were placed here

}

and this is my jsp:

<body>
    <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/>
        Address:
        <input type="text" name="currAddress"><br/>
        Email Add.:
        <input type="text" name="currEmail">
        <br/>
        <input type="submit" name="Submit" id="Submit" value="Save New Info" />
    </form>
</body>

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消