JSP第十二周作业

数据库连接

MsgDao

  1 package Dao;
  2 
  3 import java.sql.Connection;
  4 
  5 import java.util.Date;
  6 import java.sql.PreparedStatement;
  7 import java.sql.ResultSet;
  8 import java.sql.SQLException;
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 
 12 public class MsgDao extends baseDao {
 13 
 14     // 添加邮件(写邮件、回复邮件都调用,邮件状态为未读=0,时间为系统时间)
 15     public void addMsg(Msg m) {
 16         Connection con = getConnection();
 17         String sql = "insert into msg(username,sendto,title,msgcontent,msg_create_date,state) values(?,?,?,?,?,0)";
 18         PreparedStatement ps = null;
 19         try {
 20             ps = con.prepareStatement(sql);
 21             ps.setString(1, m.getUsername());
 22             ps.setString(2, m.getSendto());
 23             ps.setString(3, m.getTitle());
 24             ps.setString(4, m.getMsgcontent());
 25             ps.setDate(5, new java.sql.Date(new Date().getTime()));
 26             ps.executeUpdate();
 27         } catch (Exception e) {
 28             // TODO: handle exception
 29             e.printStackTrace();
 30         } finally {
 31             closeAll(con, ps, null);
 32         }
 33     }
 34 
 35     // 根据id删除邮件
 36     public int deleteMsg(int msgId) {
 37         int t = -1;
 38         PreparedStatement ps = null;
 39         Connection con = getConnection();
 40         String sql = "delete from msg where eid=?";
 41         try {
 42             ps = con.prepareStatement(sql);
 43             ps.setInt(1, msgId);
 44             t = ps.executeUpdate();
 45         } catch (SQLException e) {
 46             // TODO Auto-generated catch block
 47             e.printStackTrace();
 48         } finally {
 49             closeAll(con, ps, null);
 50         }
 51 
 52         return t;
 53 
 54     }
 55 
 56     // 根据id修改邮件状态(统一改成1)
 57     public Msg chakan(int msgid) {
 58         Msg m = null;
 59         Connection con = getConnection();
 60         ResultSet rs = null;
 61         PreparedStatement ps = null;
 62         String sql = "select * from msg where eid=?";
 63         try {
 64             ps = con.prepareStatement(sql);
 65             ps.setInt(1, msgid);
 66             rs = ps.executeQuery();
 67             while (rs.next()) {
 68                 m = new Msg();
 69                 m.setMsgid(rs.getInt("eid"));
 70                 m.setUsername(rs.getString("username"));
 71                 m.setTitle(rs.getString("title"));
 72                 m.setMsgcontent(rs.getString("msgcontent"));
 73                 m.setState(rs.getInt("state"));
 74                 m.setSendto(rs.getString("sendto"));
 75                 m.setMsg_create_date(rs.getDate("msg_create_date"));
 76             }
 77         } catch (SQLException e) {
 78             // TODO Auto-generated catch block
 79             e.printStackTrace();
 80         } finally {
 81             closeAll(con, ps, rs);
 82         }
 83         return m;
 84     }
 85 
 86     public int xiugai(int msgid) {
 87         int t = -1;
 88         Connection con = getConnection();
 89         PreparedStatement ps = null;
 90         String sql = "update msg set state=1 where msgid=?";
 91         try {
 92             ps = con.prepareStatement(sql);
 93             ps.setInt(1, msgid);
 94             t = ps.executeUpdate();
 95         } catch (SQLException e) {
 96             // TODO Auto-generated catch block
 97             e.printStackTrace();
 98         } finally {
 99             closeAll(con, ps, null);
100         }
101         return t;
102     }
103 
104     // 根据收件人查看邮件
105     public List<Msg> getMailByReceiver(String name) {
106         List<Msg> list = new ArrayList<Msg>();
107         Connection con = getConnection();
108         String sql = "select * from msg where username=?";
109         PreparedStatement ps = null;
110         ResultSet rs = null;
111         try {
112             ps = con.prepareStatement(sql);
113             ps.setString(1, name);
114             rs = ps.executeQuery();
115             while (rs.next()) {
116                 Msg m = new Msg();
117                 m.setMsgid(rs.getInt("eid"));
118                 m.setUsername(rs.getString("username"));
119                 m.setTitle(rs.getString("title"));
120                 m.setMsgcontent(rs.getString("msgcontent"));
121                 m.setSendto(rs.getString("sendto"));
122                 m.setMsg_create_date(rs.getDate("msg_create_date"));
123                 m.setState(rs.getInt("state"));
124                 list.add(m);
125             }
126 
127         } catch (SQLException e) {
128             // TODO Auto-generated catch block
129             e.printStackTrace();
130         } finally {
131             closeAll(con, ps, rs);
132         }
133         return list;
134     }
135 
136 }

主页

main.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 
 3 <html>
 4 <head>
 5 
 6 <title></title>
 7 <%@ page import="Dao.MsgDao"%>
 8 <%@ page import="Dao.Msg"%>
 9 <style type="text/css">
10 table {
11     border: 1px black solid;
12     width: 500px;
13 }
14 
15 tr {
16     border: 1px black solid;
17 }
18 </style>
19 </head>
20 
21 <body>
22 
23     <%
24         String name = (String) session.getAttribute("uname");
25     %>
26     <%
27         out.println(name);
28     %>的邮箱:
29     <a href="write.jsp">写邮件</a>
30     <br>
31     <%
32         MsgDao m = new MsgDao();
33         List<Msg> list = m.getMailByReceiver(name);
34     %>
35 
36     <table border="1">
37         <tr>
38             <td>发件人</td>
39             <td>收件人</td>
40             <td>标题</td>
41             <td>状态</td>
42             <td>时间</td>
43             <td>回复</td>
44             <td>删除</td>
45         </tr>
46         <%
47             for (int i = 0; i < list.size(); i++) {
48         %>
49         <tr>
50             <td><%=list.get(i).getUsername()%></td>
51             <td><a href="look.jsp?id=<%=list.get(i).getMsgid()%>"><%=list.get(i).getSendto()%></a>
52             </td>
53             <td><%=list.get(i).getTitle()%></td>
54             <td>
55                 <%
56                     if (list.get(i).getState() == 0) {
57                 %><img src="image/weidu.png" /> <%
58      } else {
59  %> <img src="image/yidu.png" /> <%
60      }
61  %>
62             </td>
63 
64             <td><%=list.get(i).getMsg_create_date()%></td>
65             <td><a href="write.jsp?reply=<%=list.get(i).getSendto()%>">回复
66             
67             </td>
68             <td><a href="delete.jsp?msgid=<%=list.get(i).getMsgid()%>">删除
69             
70             </td>
71             <%
72                 }
73             %>
74         
75     </table>
76 </body>
77 </html>

写邮件(回复邮件)

write.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 
 3 <!DOCTYPE HTML>
 4 <html>
 5 <head>
 6 <title></title>
 7 </head>
 8 <body>
 9     <form action="dowrite.jsp" method="post">
10         <table>
11             <tr>
12                 <td>收件人</td>
13                 <td><input type="text" name="sendto"
14                     value="<%=request.getParameter("reply")%>"></td>
15             </tr>
16             <tr>
17                 <td>主题</td>
18                 <td><input type="text" name="title"></td>
19             </tr>
20             <tr>
21                 <td>内容</td>
22                 <td><textarea rows="6" cols="20" name="content"></textarea></td>
23             </tr>
24             <tr>
25                 <td><input type="submit" value="发送"></td>
26             </tr>
27         </table>
28     </form>
29     <a href="main.jsp">返回</a>
30 </body>
31 </html>

dowrite.jsp

 1 <%@page import="Dao.MsgDao"%>
 2 <%@page import="Dao.Msg"%>
 3 
 4 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 5 
 6 <!DOCTYPE HTML>
 7 <html>
 8 <head>
 9 <title></title>
10 </head>
11 <body>
12     <%
13         request.setCharacterEncoding("utf-8");
14         Msg e = new Msg();
15         String username = (String) session.getAttribute("uname");
16         e.setUsername(username);
17         String sendto = request.getParameter("sendto");
18         e.setSendto(sendto);
19         String title = request.getParameter("title");
20         e.setTitle(title);
21         String msgcontent = request.getParameter("msgcontent");
22         e.setMsgcontent(msgcontent);
23         MsgDao md = new MsgDao();
24         md.addMsg(e);
25 
26         out.print("发送成功,即将跳回首页...");
27         response.setHeader("refresh", "1;url=main.jsp");
28     %>
29 
30 </body>
31 </html>

阅读邮件(点击收件人)

look.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 
 3 <html>
 4 <head>
 5 
 6 <title></title>
 7 
 8 </head>
 9 <%@ page import="Dao.MsgDao"%>
10 <body>
11     <%
12         String Id = request.getParameter("id");
13         int msgid = Integer.parseInt(Id);
14         MsgDao md = new MsgDao();
15         md.xiugai(msgid);
16         out.println(md.chakan(msgid));
17     %>
18     <br>
19     <a href="main.jsp">返回</a>
20 </body>
21 </html>

删除邮件

delete.jsp

 1 <%@page import="Dao.MsgDao"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 3 <html>
 4 <head>
 5 <title></title>
 6 </head>
 7 
 8 <body>
 9     <%
10         try {
11             String msgid = request.getParameter("msgid");
12             int msgId = Integer.parseInt(msgid);
13             MsgDao md = new MsgDao();
14             if (md.deleteMsg(msgId) != -1) {
15                 out.println("删除成功!");
16                 out.println("即将跳转到主页面...");
17                 response.setHeader("refresh", "1;url=main.jsp");
18             } else {
19                 out.println("删除失败!");
20                 out.println("即将跳转到主页面...");
21                 response.setHeader("refresh", "1;url=main.jsp");
22             }
23         } catch (Exception e) {
24             e.printStackTrace();
25         }
26     %>
27 </body>
28 </html>

截图

 

 

posted @ 2022-05-18 22:54  唐一南  阅读(21)  评论(0编辑  收藏  举报