一尘不染

无法使用Servlet和JSP更新表中的特定记录

java

大家好,我为用户创建了一个jsp页面,他们可以在其中更新现有详细信息,并在提交数据库时更新记录。

1.> Accountdetails.java

//Java Servlet program responsible for obtaining the user data and updating into   database    
package roseindia.net;

import java.io.*;
import java.sql.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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 Accountdetails 
 */
 @WebServlet("/Accountdetails")
 public class Accountdetails extends HttpServlet {
    private static final long serialVersionUID = 1L;

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


/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("Servlet is in the doGet method of account details");




}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("Servlet is in the doPost method of account details");

    response.setContentType("text/html");  
    PrintWriter pw = response.getWriter(); 
    pw.println("<html>");
    pw.println("<body>");
    pw.println("<h1>Hello Servlet Post</h1>");
    pw.println("</body>");
    pw.println("</html>");


    Connection conn=null;
    String url="jdbc:mysql://localhost:3306/";
    String dbName="userlogindb";
    String driver="com.mysql.jdbc.Driver";

    try{  
      String Fname = request.getParameter("fname");  
      String Mname = request.getParameter("mname");  
      String Lname = request.getParameter("lname");  
      String Uname = request.getParameter("username");  
      String Emailid = request.getParameter("emailid");  
      String Mobno = request.getParameter("mobno");  
      String Address = request.getParameter("address");  
      String Password1 = request.getParameter("password1");  
      String Password2 = request.getParameter("password2");
      String ID = request.getParameter("id");

      Class.forName(driver);  
      conn = DriverManager.getConnection(url+dbName,"root", "root");

          String update="update registerutable set fname='"+Fname+"', mname='"+Mname+"',    lname='"+Lname+"', username='"+Uname+"', emailid='"+Emailid+"', mobno='"+Mobno+"',    address='"+Address+"', password1='"+Password1+"', password2='"+Password2+"' where    id='"+ID+"' ";

                System.out.println("The sql is " +update);
                     PreparedStatement pst = (PreparedStatement) conn.prepareStatement(update);


      pst.setString(1,Fname);  
      pst.setString(2,Mname);        
      pst.setString(3,Lname);
      pst.setString(4,Uname);
      pst.setString(5,Emailid);
      pst.setString(6,Mobno);
      pst.setString(7,Address);
      pst.setString(8,Password1);
      pst.setString(9,Password2);
      pst.setString(11,ID);


      int i = pst.executeUpdate();

      String msg=" ";
      if(i!=0){  
        msg="User Updated";

        pw.print(i);
        pw.print("<font size='6' color=blue>" + msg + "</font>");  
        pw.print(" '"+Fname+"'");

      }  
      else{  
        msg="failed to update the data";
        pw.print("<font size='6' color=blue>" + msg + "</font>");
       }  
      pw.close();
      pst.close();
      conn.close();
    }  
    catch (Exception e){  
        e.printStackTrace(); 
    }

}

 }

2.> success.jsp

//JSP page which shows existing value and new value to be entered by user.

<form name="myaccountform" action="Accountdetails" method="post">
<table border="1">
<tr>
<td>First name:</td>
<%Object value11 = request.getAttribute("fname");%>
<td><%=value11%></td>
</tr>
<tr>
<td>Enter the new first name:</td>
<td><input type="text" name="fname" value=""></td>
</tr>
<tr>
<td>Middle name:</td>
<%Object value12 = request.getAttribute("mname");%>
<td><%=value12%></td>
</tr>
<tr>
<td>Enter the new middle name:</td>
<td><input type="text" name="mname" value=""></td>
</tr>
<tr>
<td>Last name:</td>
<%Object value13 = request.getAttribute("lname");%>
<td><%=value13%></td>
</tr>
<tr>
<td>Enter the new last name:</td>
<td><input type="text" name="lname" value=""></td>
</tr>
<tr>
<td>Username:</td>
<%Object value14 = request.getAttribute("uname");%>
<td><%=value14%></td>
</tr>
<tr>
<td>Enter new username:</td>
<td><input type="text" name="username" value=""></td>
</tr>
<tr>
<td>Email id:</td>
<%Object value15 = request.getAttribute("emailid");%>
<td><%=value15%></td>
</tr>
<tr>
<td>Enter new email id:</td>
<td><input type="text" name="emailid" value=""></td>
</tr>
<tr>
<td>Mobile number:</td>
<%Object value16 = request.getAttribute("mobno");%>
<td><%=value16%></td>
</tr>
<tr>
<td>Enter new mobile number:</td>
<td><input type="text" name="mobno" value=""></td>
</tr>
<tr>
<td>Address:</td>
<%Object value17 = request.getAttribute("address");%>
<td><%=value17%></td>
</tr>
<tr>
<td>Enter new address:</td>
<td><textarea rows="5" cols="15" name="address"></textarea></td>
</tr>
<tr>
<td>Old password:</td>
<%Object value18 = request.getAttribute("password1");%>
<td><%=value18%></td>
</tr>
<tr>
<td>Enter the new password:</td>
<td><input type="password" name="password1" value=""></td>
</tr>
<tr>
<td>Reenter the new password:</td>
<td><input type="password" name="password2" value=""></td>
</tr>
</table>
<input type="submit">
</form>

3.> web.xml

//mapping between servlet and jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>LoginExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<description>Login Servlet</description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>roseindia.net.Login</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
<description>Servlet Example</description>
<display-name>ServletExample</display-name>
<servlet-name>ServletExample</servlet-name>
<servlet-class>servletexample.com.ServletExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletExample</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>

<servlet>
<description>Account Details</description>
<display-name>Accountdetails</display-name>
<servlet-name>Accountdetails</servlet-name>
<servlet-class>roseindia.net.Accountdetails</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Accountdetails</servlet-name>
<url-pattern>/Accountdetails</url-pattern>
</servlet-mapping>
</web-app>

这里的问题是当填写所有字段并单击提交时,它无法更新它。

它进入其他部分,显示更新数据失败。

当我sys出它显示SQL是

update registerutable set 
  fname='admin11', mname='admin11', lname='admin11', username='admin11',
  emailid='admin', mobno='1111', address='admin', password1='admin',  
  password2='admin' 
where id='null'

我也试过

String ID = request.getParameter("id");

int ID = Integer.parseInt(request.getParameter("id"));

但仍然无法更新。

请帮助我纠正错误,因为我现在不知道我要去哪里。


阅读 175

收藏
2020-12-03

共1个答案

一尘不染

您的代码中有几个错误:


您的servlet似乎没有收到查询参数id

String ID = request.getParameter("id");

此声明必须导致variabl ID持有null

在您中,JSP您没有为其设置表单变量id,因此AccountDetailsservlet 不会接收它。

因此,将相同的null内容传递给update不更新数据库表中任何记录的查询语句。

update registerutable set 
  fname='admin11', mname='admin11', lname='admin11', username='admin11',
  emailid='admin', mobno='1111', address='admin', password1='admin',  
  password2='admin'
where id='null'

条件where id='null'不匹配任何记录,因此不成功。


您准备好的语句的sql字符串格式错误。您应该具有占位符PreparedStatement以将值绑定到它们。

更改:

String update=
  "update registerutable set 
   fname='"+Fname+"', mname='"+Mname+"', lname='"+Lname+"', 
   username='"+Uname+"', emailid='"+Emailid+"', mobno='"+Mobno+"',
   address='"+Address+"', password1='"+Password1+"', 
   password2='"+Password2+"' where id='"+ID+"' ";

至:

String update=
  "update registerutable set 
     fname=?, mname=?, lname=?, username=?, emailid=?, 
     mobno=?, address=?, password1=?, password2=? 
   where id=?";
2020-12-03