先设计条件界面,如下图所示
对应的jsp代码如下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<title>按照条件修改数据库中的内容</title>
</head>
<body>
<form action="lianxi24.jsp" method="post">
请选择修改记录所满足的条件<hr>
姓名:<input type="text" name="name"> <br>
性别:男<input type="radio" name="sex" value="男">
性别:女<input type="radio" name="sex" value="女"><br>
<input type="submit" value="提 交">
<input type="reset" value="取 消">
</form>
</body>
</html>
获取满足条件的数据库中的内容,jsp代码如下:
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<title>后台处理页面</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/javaweb?useUnicode=true&characterEncoding=UTF-8";
String username="root";
String password="";
Connection conn= DriverManager.getConnection(url,username,password);
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
session.setAttribute("name",name);
session.setAttribute("sex",sex);
String sql = "select * from student where sex=? and name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,sex);
pstmt.setString(2,name);
ResultSet rs = pstmt.executeQuery();
if (rs.next()){// 如果新的当前行有效,则返回 true;如果不存在下一行,则返回 false
int id = rs.getInt("id");
String name1 = rs.getString("name");
String sex1 = rs.getString("sex");
int age = rs.getInt("age");
int weight = rs.getInt("weight");
int height = rs.getInt("height");
if (rs!=null){
rs.close();
}
if (pstmt!=null){
pstmt.close();
}
if (conn!=null){
conn.close();
}
%>
<form action="lianxi25.jsp" method="post">
<table border="0" width="238" height="252" align="center">
<tr>
<td>学号</td>
<td><input name="id" value=<%=id%>></td> <%--<%=id%>>要注意此处应多一个尖括号 --%>
</tr>
<tr>
<td>姓名</td>
<td><input name="name1" value=<%=name1%>></td>
</tr>
<tr>
<td>性别</td>
<td><input name="sex1" value=<%=sex1%>></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="height" value=<%=height%>></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>
更新数据库中的内容的代码:
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<title>更新新的数据</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/javaweb?useUnicode=true&characterEncoding=UTF-8";
String username="root";
String password="";
Connection conn= DriverManager.getConnection(url,username,password);
String sql = "update student set id=?,name=?,sex=?,age=?,weight=?,height=? where name=? and sex=?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
request.setCharacterEncoding("UTF-8");
int id = Integer.parseInt(request.getParameter("id"));
String name1 = request.getParameter("name1");
String sex1 = request.getParameter("sex1");
int age = Integer.parseInt(request.getParameter("age"));
int weight = Integer.parseInt(request.getParameter("weight"));
int height = Integer.parseInt(request.getParameter("height"));
String name = (String) session.getAttribute("name");
String sex = (String) session.getAttribute("sex");
preparedStatement.setInt(1,id);
preparedStatement.setString(2,name1);
preparedStatement.setString(3,sex1);
preparedStatement.setInt(4,age);
preparedStatement.setInt(5,weight);
preparedStatement.setInt(6,height);
preparedStatement.setString(7,name);
preparedStatement.setString(8,sex);
int i = preparedStatement.executeUpdate(); //返回的数值是更新数据的条数
if (i>=1){
out.print("重写数据操作成功");
}else {
out.print("重写数据操作失败");
}
if (preparedStatement!=null){
preparedStatement.close();
}
if(conn!=null){
conn.close();
}
%>
</body>
</html>