本日学习了Java web的内容,对周一测试的系统进行了完善,添加了删除的操作,在运行测试时遇到了删除文章时会删除所有标题相同文章,在经过思考后决定对所有文章进行编号操作,在删除搜索时通过标题搜索文章,删除时则根据文章的编号进行删除,将编号设置为自增的键值,在用户创建文章时自动为文章赋予编号,可保证编号的唯一性。

Test.java新增代码:

 1 public void deletedata(String biao) {
 2         Connection connection = getConnection();
 3         PreparedStatement preparedStatement = null;
 4         try {
 5             String sql = "delete from bao where 标题 = ?";
 6             preparedStatement = connection.prepareStatement(sql);
 7             preparedStatement.setString(1, biao);
 8             preparedStatement.executeUpdate();
 9             //System.out.println("删除成功");
10 
11         } catch (SQLException e) {
12             e.printStackTrace();
13         } finally {
14             close(preparedStatement);
15             close(connection);
16         }
17     }
18 
19     public boolean isSame(String s) {
20         Connection connection = getConnection();
21         PreparedStatement preparedStatement = null;
22         ResultSet rs = null;
23         try {
24             String sql = "select * from bao";
25             preparedStatement = connection.prepareStatement(sql);
26             rs = preparedStatement.executeQuery();
27             while (rs.next()) {
28                 if (s.equals(rs.getObject(1)))
29                     return true;
30             }
31             //preparedStatement.executeUpdate();
32 
33         } catch (SQLException e) {
34             e.printStackTrace();
35         } finally {
36             close(rs);
37             close(preparedStatement);
38             close(connection);
39         }
40         return false;
41     }

删除页面和操作代码:

delete.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     <title>删除</title>
 8 </head>
 9 <body>
10 <form action="deletejudge.jsp" method="get">
11     <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
12         <br> 要删除的日记标题: <input type="text" name="biao" /> <br>
13         <br><input type="submit" value="确定" />
14         <br> <input type="button" value="返回菜单" onclick="location.href='index.jsp'" /> <br>
15     </p>
16 </form>
17 </body>
18 </html>

deletejudge.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='deletejudge1.jsp?biao=<%=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>

deletejudge1.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 <input type="button" onclick="" value="删除"/>
10 <jsp:useBean  id="util" class="test.Test" scope="page" />
11 <%
12     String biao=(String)request.getParameter("biao");
13     util.deletedata(biao);
14     out.print("<script language='javaScript'> alert('删除成功');</script>");
15     response.setHeader("refresh", "0;url=delete.jsp");
16 %>
17 <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
18     <br> <input type="button" value="返回菜单" onclick="location.href='index.jsp'" /> <br>
19 </p>
20 </body>
21 </html>