Java web 例4-7
update_stu_2_tijiao.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改记录的条件提交页面</title>
</head>
<body>
请选择修改记录所满足的条件<hr width="100%" size="3">
<form action="update_stu_2_edit.jsp" method="post"><br>
姓名:<input type="text" name="name"><br><br>
性别:男<input type="radio" value="男" name="sex">
女<input type="radio" value="女" name="sex"><br><br>
<input type="submit" value="提 交">
<input type="reset" value="取 消">
</form>
</body>
</html>
update_stu_2_edit.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改编辑页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String driverName= "com.mysql.jdbc.Driver";
String userName="root";
String userPwd="147258369";
String dbName="students";
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(driverName);
Connection conn=DriverManager.getConnection(url);
String sex=request.getParameter("sex");
String name=request.getParameter("name");
session.setAttribute("sex",sex);
session.setAttribute("name",name);
String sql="select * from students_info where sex=? and name=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,sex);
pstmt.setString(2,name);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
int id=rs.getInt("id");
String name2=rs.getString("name");
String sex2=rs.getString("sex");
int age=rs.getInt("age");
float weight=rs.getFloat("weight");
float hight=rs.getFloat("hight");
if(rs!=null){rs.close();}
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close();}
%>
<form action="update_stu_3.jsp" method="post">
<table border="0" width="238" height="252">
<tr><td>学号</td><td><input name="id" value=<%=id%>></td></tr>
<tr><td>姓名</td><td><input name="name2" value=<%=name2%>></td></tr>
<tr><td>性别</td><td><input name="sex2" value=<%=sex2%>></td></tr>
<tr><td>年龄</td><td><input name="age" value=<%=age%>></td></tr>
<tr><td>体重</td><td><input name="weight" value=<%=weight%>></td></tr>
<tr><td>身高</td><td><input name="hight" value=<%=hight%>></td></tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="提 交">
<input type="reset" value="取 消">
</td>
</tr>
</table>
</form>
<%}
else{%>
没有找到合适条件的记录!!<%
if(rs!=null){rs.close();}
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close();}
}%>
</body>
</html>
update_stu_3.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改后重写记录页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String driverName= "com.mysql.jdbc.Driver";
String userName="root";
String userPwd="147258369";
String dbName="students";
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(driverName);
Connection conn=DriverManager.getConnection(url);
String sql="update students_info set id=?,name=?,sex=?,age=?,weight=?,hight=? where name=? and sex=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
int id=Integer.parseInt(request.getParameter("id"));
String name2=request.getParameter("name2");
String sex2=request.getParameter("sex2");
int age=Integer.parseInt(request.getParameter("age"));
float weight=Float.parseFloat(request.getParameter("weight"));
float hight=Float.parseFloat(request.getParameter("hight"));
String name=(String)session.getAttribute("name");
String sex=(String)session.getAttribute("sex");
pstmt.setInt(1,id);
pstmt.setString(2,name2);
pstmt.setString(3,sex2);
pstmt.setInt(4,age);
pstmt.setFloat(5,weight);
pstmt.setFloat(6,hight);
pstmt.setString(7,name);
pstmt.setString(8,sex);
int n=pstmt.executeUpdate();
if(n>=1){%>重写数据操作成功!<br><%}
else{%>重写数据操作失败! <%=n %><br><%}
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close();}%>
</body>
</html>