Javaweb简易程序的完善:
今日完善了上周测试的web程序修改功能,做出了最终的修改功能,对于修改功能一直是我以前不太会的点,之前我会修改的函数的编写,但是不太明确在jsp文件中参数的传递,经常导致页面显示修改成功而数据库没有改变的显现,曾经一度想过将修改做成删除加增加,但是今天我在经过学习别人在修改的jsp文件中的操作,学会了修改的操作,完善了之前的测试程序。
修改部分代码
Test.java中修改部分代码:
1 public void revisedata(String biao0,String zheng,String tian,String biao) 2 { 3 Connection connection = getConnection(); 4 PreparedStatement preparedStatement=null; 5 try { 6 7 String sql = "update bao set 标题=?,内容=?,坚持天数=? where 标题=?"; 8 preparedStatement=connection.prepareStatement(sql); 9 preparedStatement.setString(1,biao); 10 preparedStatement.setString(2,zheng); 11 preparedStatement.setString(3,tian); 12 preparedStatement.setString(4,biao0); 13 preparedStatement.executeUpdate(); 14 15 } catch (SQLException e) { 16 e.printStackTrace(); 17 }finally{ 18 close(preparedStatement); 19 close(connection); 20 } 21 }
jsp文件中修改部分:
revise.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>修改</title> 5 </head> 6 <body> 7 <form action="revisejudge.jsp" method="get"> 8 <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> 9 <br> 要修改的日记标题: <input type="text" name="biao" /> <br> 10 <br><input type="submit" value="确定" /> 11 <br> <input type="button" value="返回菜单" onclick="location.href='index.jsp'" /> <br> 12 </p> 13 </form> 14 </body> 15 </html>
revisejudge.jsp
1 <%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 </head> 8 <body> 9 10 <jsp:useBean id="util" class="test.Test" scope="page" /> 11 <% 12 String biao=(String)request.getParameter("biao"); 13 if(biao==""){ 14 out.print("<script language='javaScript'> alert('输入为空'); window.history.back(-1); </script>"); 15 } 16 else if(!util.isSame(biao)) 17 { 18 out.print("<script language='javaScript'> alert('该信息不存在'); window.history.back(-1); </script>"); 19 } 20 else{ 21 %> 22 <table border="1"style="text-align:center;"> 23 <tr> 24 <td align="center" width=6%>标题</td> 25 <td align="center" width=8%>正文</td> 26 <td align="center" width=10%>天数</td> 27 <td align="center" width=3%>修改</td> 28 </tr> 29 <% 30 Connection connection = util.getConnection(); 31 PreparedStatement preparedStatement=null; 32 ResultSet rs=null; 33 try { 34 String sql = "select * from bao"; 35 preparedStatement=connection.prepareStatement(sql); 36 rs=preparedStatement.executeQuery(); 37 while(rs.next()){ 38 if(biao.equals(rs.getObject(1))) 39 { 40 %> 41 <tr> 42 <td align="center"><%=rs.getObject(1) %></td> 43 <td align="center"><%=rs.getObject(2) %></td> 44 <td align="center"><%=rs.getObject(3) %></td> 45 <td align="center"><a style="color:blue" href='revisejudge1.jsp?biao0=<%=rs.getObject(1) %>' onclick="javascript:return del()">修改</a> </td> 46 </tr> 47 48 <% 49 } 50 } 51 } catch (SQLException e) { 52 e.printStackTrace(); 53 }finally{ 54 util.close(rs); 55 util.close(preparedStatement); 56 util.close(connection); 57 } 58 %> 59 </table> 60 <% 61 } 62 %> 63 <%-- 64 界面提示 65 --%> 66 <script> 67 function del(){ 68 var r = confirm("确定修改这条吗?") 69 if (r == true) { 70 return true; 71 } else { 72 return false; 73 } 74 } 75 </script> 76 <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> 77 <br> <input type="button" value="返回菜单" onclick="location.href='index.jsp'" /> <br> 78 </p> 79 </body> 80 </html>
revisejudge1.jsp
1 <%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 </head> 8 <body> 9 <jsp:useBean id="util" class="test.Test" scope="page" /> 10 <% 11 String biao0=(String)request.getParameter("biao0"); 12 session.setAttribute("biao0",biao0); 13 %> 14 <table border="1"style="text-align:center;"> 15 <tr> 16 <td align="center" width="8%">标题</td> 17 <td align="center" width=8%>正文</td> 18 <td align="center" width=10%>天数</td> 19 20 </tr> 21 <% 22 Connection connection = util.getConnection(); 23 PreparedStatement preparedStatement=null; 24 ResultSet rs=null; 25 try { 26 String sql = "select * from bao"; 27 preparedStatement=connection.prepareStatement(sql); 28 rs=preparedStatement.executeQuery(); 29 while(rs.next()){ 30 if(biao0.equals(rs.getObject(1))) 31 { 32 %> 33 <tr> 34 <td align="center"><%=rs.getObject(1)%></td> 35 <td align="center"><%=rs.getObject(2) %></td> 36 <td align="center"><%=rs.getObject(3) %></td> 37 </tr> 38 <% 39 } 40 } 41 } catch (SQLException e) { 42 e.printStackTrace(); 43 }finally{ 44 util.close(rs); 45 util.close(preparedStatement); 46 util.close(connection); 47 } 48 %> 49 </table> 50 51 <form action="revisejudge2.jsp" method="get"> 52 <br> 53 <br> 54 修改信息 55 <br> 56 <br> 57 标题: 58 <br> 59 <input type="text" name="biao"> 60 <br> 61 <br> 62 正文: 63 <br> 64 <textarea style="width: auto" name="zheng"></textarea> 65 <br> 66 天数: 67 <br> 68 <input type="text" name="tian"> 69 <br><input type="submit" value="提交" /> 70 <br> 71 <br> <input type="button" value="返回菜单" onclick="location.href='index.jsp'" /> <br> 72 </form> 73 </body> 74 </html>
revisejudge2.jsp
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 </head> 8 <body> 9 <jsp:useBean id="util" class="test.Test" scope="page" /> 10 <% 11 request.setCharacterEncoding("UTF-8"); 12 String biao0=(String)session.getAttribute("biao0"); 13 String biao=(String)request.getParameter("biao"); 14 String zheng=(String)request.getParameter("zheng"); 15 String tian=(String)request.getParameter("tian"); 16 17 if(biao==""||zheng==""||tian=="") 18 { 19 out.print("<script language='javaScript'> alert('输入为空');window.history.back(-1);</script>"); 20 } 21 else 22 { 23 util.revisedata(biao0,zheng,tian,biao); 24 out.print("<script language='javaScript'> alert('修改成功');</script>"); 25 response.setHeader("refresh", "0;url=search.jsp"); 26 } 27 %> 28 <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px"> 29 <br> <input type="button" value="返回菜单" onclick="location.href='index.jsp'" /> <br> 30 </p> 31 </body> 32 </html>