javaweb实现数据库数据修改功能(例:密码修改)
⼀、密码修改的功能实现过程:
1、通过前端输⼊数据:⽤户名(⽤作数据库数据查询的条件),新的密码(该⽤户名的原密码修改为当前输⼊的密码);
2、点击提交按钮,将数据传送到后端;
3、后端接收数据之后进⾏相应验证;
4、验证成功则修改数据库信息;
5、修改失败则跳出反馈信息。
⼆、代码实现
❀密码修改页⾯
❀页⾯代码
<%@ page contentType="text/html;charset=GB2312"%>
<%@page language="java"import="java.util.*,l.*"import="javax.servlet.http.*"%>
<HTML>
<title>密码修改页⾯</title>
<HEAD></HEAD>
<BODY >
<br>
<a href="mine.jsp"><img src="image/箭头.png"/>返回个⼈中⼼</a>
<div align="center">
<br>
<br>
<br>
<br>
<font size=4xp color=white><b>   修改密码</b></font><br><br><br>
<FORM action="HandlePassword" Method="post">
<font color=white size=4xp>登录名称: </font><Input type=text name="logname"><br><br>
<font color=white size=4xp>修改密码: </font><Input type=password name="password"><br><br>
<font color=white size=4xp>确认密码: </font><Input type=password name="again_password"><br><br>
<input type="submit" value="提交" />
<td><Input type=reset name="g" value="重置" ></td></tr>
</form>
</BODY>
</HTML>
❀后端代码
servlet:
l;
import mybean.data.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class HandlePassword extends HttpServlet
{
public void init(ServletConfig config)throws ServletException {
super.init(config);
try{  Class.forName("sql.jdbc.Driver");
}
catch(Exception e){}
}
public String handleString(String s)
{try{byte bb[]=s.getBytes("iso-8859-1");
s=new String(bb);
}
catch(Exception ee){}
return s;
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ this.doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String uri="jdbc:mysql://127.0.0.1:3306/toy_db?"+"user=root&password=123456&characterEncoding=gb2312";
Connection con;
PreparedStatement sql;
String logname = Parameter("logname").trim();
String password = Parameter("password").trim();
String again_Parameter("again_password").trim();
response.setContentType("text/html;charset=GB2312");
Login loginBean=null;
HttpSession Session(true);
loginBean=(Attribute("loginBean");
String Logname();
if(!lname.equals(logname)){
PrintWriter Writer();
out.println("<html><body>");
out.println("<h2>请输⼊当前使⽤的⽤户名<br></h2>");
out.println("<a href=changePassword.jsp>返回</a>");
out.println("</body></html>");
return;
}
else{
//传⼊参数
if(!password.equals(again_password)){
PrintWriter Writer();
PrintWriter Writer();
out.println("<html><body>");
out.println("<h2>两次密码输⼊不⼀致,密码修改失败!<br></h2>");
out.println("<a href=changePassword.jsp>返回</a>");
out.println("</body></html>");
return;
}
boolean boo=password.length()>0;
try{
Connection(uri);
String change="update client set password=?"+"where logname=?";
sql=con.prepareStatement(change);
if(boo){
sql.setString(1,handleString(password));
sql.setString(2,handleString(logname));
int uteUpdate();
if(m!=0){
RequestDispatcher RequestDispatcher("/change_sucess.jsp");//转发            dispatcher.forward(request,response);
}
}
else{
PrintWriter Writer();
out.println("<html><body>");
out.println("<h2>密码修改失败!<br></h2>");
out.println("<a href=changePassword.jsp>返回</a>");
out.println("</body></html>");
return;
}
xp提交更改}
catch(SQLException exp){
exp.printStackTrace();
}
}
}
}
bean:
package mybean.data;
import java.util.*;
import java.util.Date;
public class Login {
private String logname="",
password;
public void setLogname(String logname){
this.logname = logname;
}
public String getLogname(){
return logname;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return password;
}
}
上图中红框的代码为密码修改失败之后以新的页⾯形式输出反馈信息。
上图代码⽬的为获取前端输⼊的数据。❀跳转页⾯
密码修改成功之后通过跳转页⾯跳转到登录页⾯:
❀跳转页⾯代码
<%@ page contentType="text/html;charset=GB2312"%>
<%@page language="java"import="java.util.*"import="javax.servlet.http.*"%> <html>
<title>密码修改成功页⾯</title>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=GB2312"/>
<!--<meta http-equiv="refresh" content="5;url=login.jsp">-->
</head>
<body >
<font color=green size=5xp>密码修改成功!</font>
<script type="text/javascript">
var t=5;
setInterval('jump()',1000);
function jump(){
if(t==0){
location="login.jsp";
}
}
</script>
<span id=show></span>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。