大家好,我为用户创建了一个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"));
但仍然无法更新。
请帮助我纠正错误,因为我现在不知道我要去哪里。
您的代码中有几个错误:
您的servlet似乎没有收到查询参数id
。
String ID = request.getParameter("id");
此声明必须导致variabl 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=?";
我已经从向量到一个文本框,这是可编辑的
几周来,我一直在努力让Jasper reports工作。它在Eclipse中工作得很好,Jasper网站的演示也很好。在JBoss中,我的jsp页面无法使用它。 我的jsp页面非常基本。它调用类中的方法。它肯定达到了这一点,因为我有输出诊断。当我调用任何Jasper方法时,都会出现以下错误: 我已经到处转储jar文件。我甚至将这组jar转储到我的文件夹中。 蜡染-1.5 commons-beanu
我有2个问题 1)我有一个login.jsp页和索引页。一旦我验证了用户(在servlet ie中),用户将被重定向到index.jsp页面。我用servlet的这条线重定向它 这个过程运行良好。但是当我刷新页面时,我会被重定向回登录页面。另外,我注意到在index.jsp页面上,我得到的名字而不是index.jsp,即我的servlet名称得到displayed.for这样的例子得到代替显示 2
查找特定记录并更新
问题内容: 我可以访问名为road.mdb的数据库。在road.mdb内部,我有一个链接的SQL表, 该表的名称为Student。 我可以在MSAccess中使用查询设计插入记录 但我无法更新或删除 当在下面运行删除查询时,错误是:无法从指定的表中删除 当我在下面运行更新查询时,错误是:操作必须使用可更新的查询 我具有对sql数据库的完全访问权限,并且可以使用sql management stud
我想获取一个使用。与相比,仅锁定指定表中的行,并且连接的行不会被阻塞。但是,当我尝试使用以下代码段使用ecto执行此操作时,它失败了。 原因是ecto为使用别名,例如,这意味着我还必须在lock表达式中使用别名才能使其正常工作。 使用别名看起来不是一种正确的方法。还有其他方法可以让它工作吗?