简体   繁体   English

java中的MySQL更新方法无法正常工作

[英]MySQL update method in java not working correctly

I have written the following UPDATE method for my MySQL database.我已经为我的 MySQL 数据库编写了以下 UPDATE 方法。

public int update(User u) {

    int status = 0;
    Connection connection = null;
    PreparedStatement stm = null;

    try {
        connection = ConnectionConfiguration.getConnection();
        stm = connection.prepareStatement("UPDATE users SET f_name=? 
              l_name=? WHERE id=?");

        stm.setString(1, u.getName());
        stm.setString(2, u.getLname());
        stm.setInt(3, u.getId());

        status = stm.executeUpdate();

    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        if(stm != null) {
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    } return status;

}

Then I update the data in a JSP class like below:然后我更新 JSP 类中的数据,如下所示:

<% int id =Integer.parseInt(request.getParameter("Userid")); 
   UserM um = new UserM(); 
   User u = new User();
   u = um.select(id);
%>
<form method="GET" action="EditServlet">
<input type="hidden" name="id" value="<%=u.getId()%>">
Name:<input type="text" name="fname" value="<%=u.getName()%>"><br>
Last name:<input type="text" name="lname" value="<%=u.getLname()%>"><br>

<input type="submit" value="Edit">
</form>

In the EditServlet I have written the following code.在 EditServlet 中,我编写了以下代码。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String rid=request.getParameter("id");  
    int id=Integer.parseInt(rid);  
    String fname =request.getParameter("fname");  
    String lname=request.getParameter("lname");  


    User u =new User();  
    u.setName(fname);  
    u.setLname(lname); 
    u.setId(id);
    UserM um = new UserM(); 
    int up_status = um.update(u);  

    if(up_status > 0) {

        request.getRequestDispatcher("/GetUserInfo").forward(request, response); 
    } else {  
        request.setAttribute("errorMessage", "Could not change info");
        request.getRequestDispatcher("/GetUserInfo").forward(request, response);  
    }

}  

After I run the code and update the data it shows the error message everytime.在我运行代码并更新数据后,它每次都会显示错误消息。 Is there anything wrong with the implementation of my code?我的代码的实现有什么问题吗?

In this code在这段代码中

User u =new User();  
u.setName(fname);  
u.setLname(lname);

you are not setting the value of the User::id , so when you go to use it in update(User u) the value will be zero.您没有设置User::id的值,因此当您在update(User u)使用它时,该值将为零。

change to改成

User u =new User();  
u.setName(fname);  
u.setLname(lname);
u.setId (id);

The problem was with my SQL statement.问题出在我的 SQL 语句上。 I had forgotten a comma after f_name.我忘记了 f_name 后面的逗号。 It should have been: "UPDATE users SET f_name=?, l_name=? WHERE id=?"它应该是:“UPDATE users SET f_name=?, l_name=? WHERE id=?”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM