更新查询在servlet

时间:2018-03-07 08:06:32

标签: java mysql servlets jdbc sql-update

大家好,当我尝试更新数据库中的任何记录时,它显示已成功更新,但是当我检查数据库时,它还没有更新。

代码中没有错误但它无法正常工作。任何人都可以调查这一点,并在代码中指出问题。

我正在尝试使用电话号码更新用户名作为唯一值。

谢谢。

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

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

2 个答案:

答案 0 :(得分:3)

您似乎错误地设置了Preparedstatement参数

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

应该是

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

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

答案 1 :(得分:2)

您缺少查询中参数的顺序第一个参数需要用户名值,第二个参数需要电话号码值。因此,改变它将起作用的参数顺序

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