<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>修改提交页面</title> </head> <body> <center> 请选择修改记录所满足的条件 <form action="stuupdate2.jsp" method="post"> 姓名: <input type="text"><br><br> 性别: 男<input type="radio" value="男"> 女<input type="radio" value="女"><br><br> <input type="submit" value="提交"> <input type="reset" value="取消"> </form> </center> </body> </html>
<%@ page contentType="text/html" import="java.sql.*" pageEncoding="UTF-8"%> <html> <head> <title>修改编辑页面</title> </head> <body> <% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String user="sa"; String password="123456"; String url="jdbc:sqlserver://localhost:1433;DatabaseName=p"; Connection conn=DriverManager.getConnection(url,user,password); request.setCharacterEncoding("UTF-8"); String name=request.getParameter("name"); String sex=request.getParameter("sex"); session.setAttribute("sex",sex); session.setAttribute("name",name); String sql="select * from Student where Ssex=? and Sname=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1,sex); pstmt.setString(2,name); ResultSet rs=pstmt.executeQuery(); if(rs.next()){ String id=rs.getString("Sno"); String name2=rs.getString("Sname"); String sex2=rs.getString("Ssex"); int age=rs.getInt("Sage"); String sdept=rs.getString("Sdept"); if(pstmt!=null){pstmt.close();} if(rs!=null){rs.close();} if(conn!=null){conn.close();} %> <form action="stuupdate3.jsp" method="post"> <table> <tr><td>学号:</td><td><input type="text" value=<%=id%>></td></tr> <tr><td>姓名:</td><td><input type="text" value=<%=name2%>></td></tr> <tr><td>性别:</td><td><input type="text" value=<%=sex2%>></td></tr> <tr><td>年龄:</td><td><input type="text" value=<%=age%>></td></tr> <tr><td>专业:</td><td><input type="text" value=<%=sdept%>></td></tr> <tr> <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 language="java" import="java.sql.*" pageEncoding="UTF-8"%> <html> <head> <title>修改后重写页面</title> </head> <body> <% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url="jdbc:sqlserver://localhost:1433; DatabaseName=p"; String user="sa"; String password="123456"; Connection conn=DriverManager.getConnection(url,user,password); String sql="update Student set Sno=?,Sname=?,Ssex=?,Sage=?,Sdept=? where Sname=? and Ssex=?"; PreparedStatement pstmt=conn.prepareStatement(sql); request.setCharacterEncoding("UTF-8"); String id=request.getParameter("id"); String name2=request.getParameter("name2"); String sex2=request.getParameter("sex2"); int age=Integer.parseInt(request.getParameter("age")); String sdept=request.getParameter("sdept"); String name=(String)session.getAttribute("name"); String sex=(String)session.getAttribute("sex"); pstmt.setString(1,id); pstmt.setString(2,name2); pstmt.setString(3,sex2); pstmt.setInt(4,age); pstmt.setString(5,sdept); pstmt.setString(6,name); pstmt.setString(7,sex); int n=pstmt.executeUpdate(); if(n>=1){%>重写数据库操作成功!<br><%} else{%>重写数据库操作失败!<br><%} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %> </body> </html>
4.删除记录模块