嘿伙计我为用户创建了一个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&GT;的 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&GT;的的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>
这里的问题是当填满所有字段并点击提交时它无法更新它。
进入其他部分显示无法更新数据。
当我退出时,显示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"));
但仍然无法更新。
请帮我纠正,因为我现在不知道我哪里错了。
答案 0 :(得分:1)
您的代码中存在多个错误:
您的servlet似乎没有收到查询参数id
。
String ID = request.getParameter("id");
此声明必须导致变量ID
保留null
。
在JSP
中,您没有为id
设置表单变量,因此AccountDetails
servlet没有收到它。
因此,相同的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=?";
答案 1 :(得分:1)
您的代码:
pst.setString(9,Password2);
pst.setString(11,ID);
你犯了一些错误:
pst.setString(9,Password2);
pst.setString(10,ID);
你应该看看以下这一行:
此字段名称,如:(故障页面)
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);
现在你明白了你的错误,你应该改变它并编译它。
String update=
"update registerutable set
fname=?, mname=?, lname=?, username=?, emailid=?,
mobno=?, address=?, password1=?, password2=?
where id=?";
答案 2 :(得分:0)
由于列ID
的mysql datatbase中的数据类型为int
所以改变pst.setString(11,ID);
到pst.setInt(11,ID);
但ID是一个字符串,因此您需要使用int
将其转换为Integer.parseInt();
答案 3 :(得分:0)
您可以尝试通过使用问号替换变量名来修改对引用更新的字符串的赋值,因为您正在使用预准备语句来执行表更新。