修改提交表的数据并保存到数据库
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>修改记录的条件提交页面</title> 8 </head> 9 <body> 10 请选择修改记录所满足的条件<hr width="100%" size="3"> 11 <form action="update_stu_2_edit.jsp" method="post"> 12 姓名:<input type="text" name="name"><br> <br> 13 性别:男<input type="radio" value="男" name="sex"> 14 女<input type="radio" value="女" name="sex"><br> <br> 15 <input type="submit" value="提 交"> 16 17 <input type="reset" value="取 消"> 18 </form> 19 </body> 20 </html>
1 <%@page import="java.sql.*"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>修改编辑页面</title> 9 </head> 10 <body> 11 <% 12 String className="com.mysql.jdbc.Driver"; 13 String url="jdbc:mysql://localhost:3306/db_shangke"; 14 String user="root"; 15 String password="root"; 16 17 Class.forName(className); 18 Connection conn=DriverManager.getConnection(url, user, password); 19 request.setCharacterEncoding("UTF-8"); 20 String sex=request.getParameter("sex"); 21 String name=request.getParameter("name"); 22 23 session.setAttribute("sex", sex); 24 session.setAttribute("name", name); 25 26 String sql="select *from stu_info where sex=? and name=?"; 27 PreparedStatement pstmt=conn.prepareStatement(sql); 28 pstmt.setString(1, sex); 29 pstmt.setString(2, name); 30 ResultSet rs=pstmt.executeQuery(); 31 if(rs.next()){ 32 int id=rs.getInt(1); 33 String name2=rs.getString("name"); 34 String sex2=rs.getString(3); 35 int age=rs.getInt("age"); 36 float weight=rs.getFloat(5); 37 float height=rs.getFloat(6); 38 if(rs!=null){ 39 rs.close(); 40 } 41 if(pstmt!=null){ 42 pstmt.close(); 43 }if(conn!=null){ 44 conn.close(); 45 } 46 47 %> 48 <form action="update_stu_2.jsp" method="post"> 49 <table border="0" width="238" height="252"> 50 <tr> 51 <td> 学号 </td> <td> <input name="id" value=<%=id%>> </td> 52 </tr> 53 <tr> 54 <td> 姓名 </td> <td> <input name="name2" value=<%=name2%>> </td> 55 </tr> 56 <tr> 57 <td> 性别 </td> <td> <input name="sex2" value=<%=sex2%>> </td> 58 </tr> 59 <tr> 60 <td> 年龄 </td> <td> <input name="age" value=<%=age%>> </td> 61 </tr> 62 <tr> 63 <td> 体重 </td> <td> <input name="weight" value=<%=weight%>> </td> 64 </tr> 65 <tr> 66 <td> 身高 </td> <td> <input name="height" value=<%=height%>> </td> 67 </tr> 68 <tr align="center"> 69 <td colspan="2"> 70 <input type="submit" value="提 交"> 71 <input type="reset" value="取 消"> 72 </td> 73 </tr> 74 75 <%} 76 else{ 77 %> 78 没有找到合适的条件记录!! 79 <% 80 if(rs!=null){ 81 rs.close(); 82 } 83 if(pstmt!=null){ 84 pstmt.close(); 85 }if(conn!=null){ 86 conn.close();} 87 } 88 %> 89 90 </table> 91 </form> 92 </body> 93 </html>
1 <%@page import="java.sql.*"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>修改后重写记录页面</title> 9 </head> 10 <body> 11 <% 12 String className="com.mysql.jdbc.Driver"; 13 String url="jdbc:mysql://localhost:3306/db_shangke"; 14 String user="root"; 15 String password="root"; 16 17 Class.forName(className); 18 19 Connection conn=DriverManager.getConnection(url, user, password); 20 String sql="update stu_info set id=?,name=?,sex=?,age=?,weight=?,height=? where name=? and sex=?"; 21 22 PreparedStatement pstmt=conn.prepareStatement(sql); 23 24 request.setCharacterEncoding("UTF-8"); 25 int id=Integer.parseInt(request.getParameter("id")); 26 String name2=request.getParameter("name2"); 27 String sex2=request.getParameter("sex2"); 28 int age=Integer.parseInt(request.getParameter("age")); 29 float weight=Float.parseFloat(request.getParameter("weight")); 30 float height=Float.parseFloat(request.getParameter("height")); 31 32 String name=(String)session.getAttribute("name"); 33 String sex=(String)session.getAttribute("sex"); 34 35 pstmt.setInt(1, id); 36 pstmt.setString(2, name2); 37 pstmt.setString(3, sex2); 38 pstmt.setInt(4,age); 39 pstmt.setFloat(5, weight); 40 pstmt.setFloat(6, height); 41 42 43 pstmt.setString(7,name); 44 pstmt.setString(8,sex); 45 46 int n=pstmt.executeUpdate(); 47 //System.out.print(n); 48 if(n>=1){ 49 out.print("重写数据成功!"+"<br>"); 50 } 51 else{ 52 out.print("重写数据失败!"+"<br>"); 53 out.print("n="+n); 54 } 55 56 if(pstmt!=null){ 57 pstmt.close(); 58 } 59 if(conn!=null){ 60 conn.close(); 61 } 62 %> 63 </body> 64 </html>
道阻且长,行则将至

浙公网安备 33010602011771号