对数据库student中数据表stu_info满足条件的记录进行修改
实现查询条件的提交:update_stu_2_tijiao.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<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="man" name="sex">
女<input type="radio" value="woman" name="sex"><br><br>
<input type="submit" value="提 交">
<input type="reset" value="取 消">
</form>
</body>
</html>
实现对满足条件的记录信息返回编辑页面并修改,待编辑修改修改完成后提交:update_stu_2_edit.jsp
<%@ page contentType="text/html" import="java.sql.*" pageEncoding="utf-8"%>
<html>
<body>
<%String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPwd="123456";
String dbName="student";
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);
request.setCharacterEncoding("UTF-8");
String sex=request.getParameter("sex");
String name=request.getParameter("name");
session.setAttribute("sex", sex);
session.setAttribute("name", name);
String sql="select * from stu_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_2.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="name" value=<%=name %>></td></tr>
<tr><td>性别</td><td><input name="sex" value=<%=sex %>></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_2.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
<html>
<body>
<%String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPwd="123456";
String dbName="student";
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 stu_info set id=?,name=?,age=?,weight=?,hight=? where name=? and sex=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
request.setCharacterEncoding("utf-8");
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>