JDBC更新页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改记录的条件提交页面</title>
</head>
<body>
	请选择修改记录所满足的条件<hr width="100%" size="3">
	<form action="更新2.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="提交" >
		&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="reset" value="取消 ">
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改编辑页面</title>
</head>
<body>
	<%
	String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	String user="sa";
	String password="zqb19971023";
	String url="jdbc:sqlserver://localhost:1433;DatabaseName=666";
	Connection conn=null;
	try{
		Class.forName(driver);
		conn=DriverManager.getConnection(url, user, password);
		System.out.println("连接成功");
	}catch(Exception e)
	{
		System.out.println("连接失败");
	}
	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 stu1 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="更新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 copspan="2">
					<input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp;
					<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 language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改后重写记录页面</title>
</head>
<body>
	<%
	String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	String user="sa";
	String password="zqb19971023";
	String url="jdbc:sqlserver://localhost:1433;DatabaseName=666";
	Connection conn=null;
	try{
		Class.forName(driver);
		conn=DriverManager.getConnection(url, user, password);
		System.out.println("连接成功");
	}catch(Exception e)
	{
		System.out.println("连接失败");
	}
	request.setCharacterEncoding("UTF-8");
	String sql="update stu1 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>

猜你喜欢

转载自blog.csdn.net/abc1498880402/article/details/83148866
今日推荐