十二
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.dmk.entity.Msg; public class MsgDao extends BaseDao { public void addMsg(Msg m) { Connection conn = getConnection(); String sql = "insert into msg(uname,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)"; PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); ps.setString(1, m.getUsername()); ps.setString(2, m.getTitle()); ps.setString(3, m.getMsgcontent()); ps.setInt(4, 1); ps.setString(5, m.getSendto()); ps.setDate(6, new java.sql.Date(new Date().getTime()));// 系统当前时间 ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } } public void delMail(int id) { Connection conn = getConnection(); String sql = "delete from msg where eid=?"; PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(conn, ps, null); } } public void updateStateById(int id){ Connection conn = getConnection(); String sql = "update msg set state = 1 where eid = ?"; PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public List<Msg> getMailByReceiver(String name) { List<Msg> list = new ArrayList<Msg>(); Connection conn = getConnection(); String sql = "select * from msg where sendto=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1, name); rs = ps.executeQuery(); while (rs.next()) { Msg m = new Msg(); m.setMsgid(rs.getInt("eid")); m.setUsername(rs.getString("uname")); m.setTitle(rs.getString("title")); m.setMsgcontent(rs.getString("msgcontent")); m.setState(rs.getInt("state")); m.setSendto(rs.getString("sendto")); m.setMsg_create_date(rs.getDate("msg_create_date")); list.add(m); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, rs); } return list; } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% String uname = (String) session.getAttribute("uname"); %> 首页!!欢迎你<%=uname%> <a href="write.jsp">写邮件</a> <table border="1"> <tr> <td>发件人</td> <td>主题</td> <td>状态</td> <td>时间</td> <td>操作</td> <td>操作</td> <td>操作</td> <% MsgDao md = new MsgDao(); List<Msg> list = md.getMailByReceiver(uname); for (int i = 0; i < list.size(); i++) { %> <tr> <td><%=list.get(i).getUsername()%></td> <td><%=list.get(i).getTitle()%></td> <td> <% if (list.get(i).getState() == 1) { %> <img src="images/sms_unReaded.png" /> <% } else { %> <img src="images/sms_readed.png" /> <% } %> </td> <td><%=list.get(i).getMsg_create_date()%></td> <td><a href="delete.jsp?id=<%=list.get(i).getMsgid()%>">删除</a></td> <td><a href="write.jsp?reply=<%=list.get(i).getUsername()%>">回复</a> </td> <td><a href="detail.jsp?<%=getMsg(list.get(i))%>">查看</a></td> </tr> <% } %> </table> <%!String getMsg(Msg msg) { return "msgid=" + msg.getMsgid() + "&uname=" + msg.getUsername() + "&time=" + msg.getMsg_create_date() + "&title=" + msg.getTitle() + "&msgcontent=" + msg.getMsgcontent(); }%> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <form action="dowrite.jsp" method="post"> 收件人:<input type="text" name="receiver" value="<%=request.getParameter("reply")%>"><br> 主题: <input type="text" name="title" ><br> 内容 <textarea rows="6" cols="20" name="content"></textarea> <br> <input type="submit" value="发送"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% request.setCharacterEncoding("utf-8"); String uname=(String)session.getAttribute("uname"); String sendto=request.getParameter("receiver"); String title=request.getParameter("title"); String content=request.getParameter("content"); Msg m=new Msg(); m.setMsgcontent(content); m.setUsername(uname); m.setSendto(sendto); m.setTitle(title); MsgDao md=new MsgDao(); md.addMsg(m); out.print("发送成功....."); response.setHeader("refresh", "5;url=main.jsp"); %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); %> <!DOCTYPE HTML> <html> <head> <title>My JSP 'msg.jsp' starting page</title> </head> <% int id = Integer.parseInt(request.getParameter("msgid")); String uname = new String(request.getParameter("uname").getBytes("ISO-8859-1"),"UTF-8"); String title = new String(request.getParameter("title").getBytes("ISO-8859-1"),"UTF-8"); String time = new String(request.getParameter("time").getBytes("ISO-8859-1"),"UTF-8"); String msgcontent = new String(request.getParameter("msgcontent").getBytes("ISO-8859-1"),"UTF-8"); MsgDao msgDao = new MsgDao(); msgDao.updateStateById(id); %> <body> 发件人:<input type="text" name="receiver" value="<%=uname %>" readonly="readonly"><br> 时间:<input type="text" name="title" value="<%=time %>" readonly="readonly"><br> 主题:<input type="text" name="title" value="<%=title %>" readonly="readonly"><br> 内容:<textarea rows="6" cols="20" name="content" readonly="readonly"><%=msgcontent %></textarea><br> <a href="main.jsp">返回</a> </body> </html>