第十二周作业

<%@page import="com.gd.dao.MsgDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <%
        request.setCharacterEncoding("utf-8");

        int id = Integer.parseInt(request.getParameter("id"));
        MsgDao md = new MsgDao();
        md.delMail(id);
        out.print("刪除成功......");
        response.setHeader("refresh", "3;url=main.jsp");
        //response.sendRedirect("main.jsp");
    %>

</body>
</html>
package com.gd.dao;

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.gd.entity.Msg;

public class MsgDao extends BaseDao {
    // 1,插入邮件
    public void addMsg(Msg m) {
        Connection conn = getConnection();
        String sql = "insert into msg(username,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);
        }

    }
     // 2.删除邮件
    public void delMail(int id) {
        Connection conn = getConnection();
        String sql = "delete from msg where msgid=?";
        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);
        }

    }

    // 3.修改邮件状态
    public void update(int id) {
        Connection con = getConnection();
        String sql = "update  msg set state='1' where msgid=?";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, null);
        }
    }

    // 4.按照接收者查询全部邮件
    public List<Msg> getMailByReceiver(String name) {
        List<Msg> list = new ArrayList<Msg>();
        Connection con = getConnection();
        String sql = "select * from msg where sendto=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            while (rs.next()) {
                Msg m = new Msg();
                m.setMsgid(rs.getInt(1));
                m.setUsername(rs.getString(2));
                m.setTitle(rs.getString(3));
                m.setMsgcontent(rs.getString(4));
                m.setState(rs.getInt(5));
                m.setSendto(rs.getString(6));
                m.setMsg_create_date(rs.getDate(7));
                list.add(m);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }
        return list;

    }
  //5.实现阅读邮件功能
    public Msg read(int id) {
        Connection con = getConnection();
        String sql = "select msgid,username,sendto,title,msgcontent,msg_create_date from msg where msgid=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                Msg m = new Msg();
                m.setMsgid(rs.getInt("msgid"));
                m.setUsername(rs.getString("username"));
                m.setTitle(rs.getString("title"));
                m.setMsgcontent(rs.getString("msgcontent"));
                m.setSendto(rs.getString("sendto"));
                m.setMsg_create_date(rs.getDate("msg_create_date"));
                return m;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return null;
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</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="发送"> <br> <a
            href="main.jsp">返回</a>

    </form>
</body>
</html>
<%@page import="com.gd.entity.Msg"%>
<%@page import="com.gd.dao.MsgDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
        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", "3;url=main.jsp");
    %>

</body>
</html>
<%@page import="com.gd.entity.Msg"%>
<%@page import="com.gd.dao.MsgDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <%
        request.setCharacterEncoding("utf-8");
        String msgid = request.getParameter("id");
        int idd = Integer.parseInt(msgid);
        MsgDao md = new MsgDao();
        md.update(idd);
        Msg m =md.read(idd);
    %>
    <table>
        <tr>
            <td>发件人:</td>
            <td><input type="text" name="username" style="border: none"
                value="<%=m.getUsername()%>">
            </td>
        </tr>
        <tr>
            <td>主题:</td>
            <td><input type="text" name="title" style="border: none"
                value="<%=m.getTitle()%>">
            </td>
        </tr>
        <tr>
            <td>时间:</td>
            <td><input type="text" name="msg_create_date" style="border: none"
                value="<%=m.getMsg_create_date()%>">
            </td>
        </tr>
        <tr>
            <td>收件人:</td>
            <td><input type="text" name="sendto" style="border: none"
                value="<%=m.getSendto()%>">
            </td>
        </tr>
        <tr>
            <td>内容:</td>
            <td><div style="border: none;outline: none;overflow: inherit;">
                    <%=m.getMsgcontent()%></div>
            </td>
        </tr>
    </table>
    <br>
    <a href="main.jsp">返回</a>

</body>
</html>

 

 

 

posted @ 2022-05-22 21:35  王智达  阅读(9)  评论(0编辑  收藏  举报