无法使用servlet和jsp更新表中的特定记录 [英] Unable to update a particular record in table using servlet and jsp

查看:95
本文介绍了无法使用servlet和jsp更新表中的特定记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们我为用户创建了一个jsp页面,他们可以在这里更新现有的详细信息,并在提交时在数据库中更新记录。

Hey guys I have created a jsp page for user where they can update their existing details and the record get updated in database on submit.

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.> successful.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>

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

The problem here is when fill up all the fields and click on submit it is unable to update it.

它进入其他部分显示无法更新数据。

It goes into else part showing failed to update the data.

当我出来时它显示了sql是

When I sys out it shows The sql is

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

但仍然无法更新。

请帮我纠正,因为我不知道我现在哪里出错。

Please help me to correct as I dont know where m I going wrong now.

推荐答案

你的代码中有几个错误:

There are several errors in your code:

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

Your servlet does not seem to be receiving the query parameter id.

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

此声明必须导致变量 ID 到持有 null

This statement must be causing variabl ID to hold a null.

JSP 中,你是没有为 id 设置表单变量,因此 AccountDetails servlet没有收到它。

In your JSP you are not setting a form variable for id and hence it is not being received by AccountDetails servlet.

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

And hence the same null is passed to update query statement which is not updating any record in the database table.

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

条件其中id =' null'未匹配任何记录,因此未成功。

The condition where id='null' is not matching any records and hence not succeeded.

您准备的sql字符串陈述错误形成。你应该有 PreparedStatement 的占位符来绑定它们的值。

Your sql string for prepared statement is wrongly formed. You should have place holders for PreparedStatement to bind values with them.

更改:

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=?";   

这篇关于无法使用servlet和jsp更新表中的特定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆