简体   繁体   English

更新查询无法在servlet中正常工作

[英]Update query not working properly in servlet

Hi all when I try to update any record in the database it shows successfully updated however when I check the database it is not yet updated. 大家好,当我尝试更新数据库中的任何记录时,它显示已成功更新,但是当我检查数据库时,它尚未更新。

There is no error in the code but it is not working. 代码中没有错误,但无法正常工作。 Can anyone look into this and point out the problem in the code. 任何人都可以调查一下并指出代码中的问题。

I am trying to update the username using the phone number as an unique value. 我正在尝试使用电话号码作为唯一值来更新用户名。

Thank you. 谢谢。

Updatelogin.html Updatelogin.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>

<body>
<form action ="update" method="post">
       phone : <input type ="text" name ="phone" /> </br> 
 Username : <input type ="text" name ="uname"/> </br>


<input type ="submit" value = "update">

</form>
</body>
</html>

Update.java 更新.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class update
 */
@WebServlet("/update")
public class update extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public update() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    public void doGet(HttpServletRequest request,HttpServletResponse response) 
            throws IOException,ServletException{
                processRequset (request,response);
            }

            public void doPost(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {

                processRequset (request,response);
            }

            public void processRequset(HttpServletRequest request,HttpServletResponse response) 
                    throws IOException,ServletException{
                response.setContentType("text/html;charset=UTF-8");

                String phone =request.getParameter("phone");
                String username =request.getParameter("uname");

                PrintWriter out = response.getWriter();


        try {




            Class.forName("com.mysql.jdbc.Driver");  
            Connection con=DriverManager.getConnection(  
            "jdbc:mysql://localhost:3306/register","root","toor");  

            PreparedStatement pstmt=con.prepareStatement(  
            "update headwy set uname =? where phone=?"); 
             pstmt.setString(1,phone);
             pstmt.setString(2,username);


             pstmt.executeUpdate();  

            out.print("You are successfully update...");  
            pstmt.close();
            con.close();

        }
          catch (Exception e2) {System.out.println(e2);
           }  
            }
}

It seems you set Preparedstatement argument wrongly 看来您错误地设置了Preparedstatement参数

pstmt.setString(1,phone);
pstmt.setString(2,username);

it should be 它应该是

pstmt.setString(1,username);
pstmt.setString(2,phone);

Check your query "update headwy set uname =? where phone=?"); 检查您的查询“ update headwy set uname =?where phone =?”);

You are missing order of the parameters in your query 1st parameter requires username value and 2nd parameter requires phone number value. 您在查询中缺少参数的顺序第一个参数需要用户名值,第二个参数需要电话号码值。 So, change order of parameters it will works 因此,更改参数的顺序将起作用

PreparedStatement pstmt=con.prepareStatement(  
            "update headwy set uname =? where phone=?"); 
             pstmt.setString(1,username);
             pstmt.setString(2,phone);

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

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