其他分享
首页 > 其他分享> > JSP第十二周

JSP第十二周

作者:互联网

package com.ym.entity;

import java.util.Date;

public class Msg {
    private int msgid;
    private String username;
    private String title;
    private String msgcontent;
    private int state;
    private String sendto;
    Date msg_create_date;

    public int getMsgid() {
        return msgid;
    }

    public void setMsgid(int msgid) {
        this.msgid = msgid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMsgcontent() {
        return msgcontent;
    }

    public void setMsgcontent(String msgcontent) {
        this.msgcontent = msgcontent;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getSendto() {
        return sendto;
    }

    public void setSendto(String sendto) {
        this.sendto = sendto;
    }

    public Date getMsg_create_date() {
        return msg_create_date;
    }

    public void setMsg_create_date(Date msg_create_date) {
        this.msg_create_date = msg_create_date;
    }
    

    public Msg(int msgid, String username, String title, String msgcontent,
            int state, String sendto, Date msg_create_date) {
        super();
        this.msgid = msgid;
        this.username = username;
        this.title = title;
        this.msgcontent = msgcontent;
        this.state = state;
        this.sendto = sendto;
        this.msg_create_date = msg_create_date;
    }
    
    

    public Msg() {
        super();
    }

    @Override
    public String toString() {
        return "Msg [msgid=" + msgid + ", username=" + username + ", sendto=" + sendto + ", title="
                + title + ", msg_create_date=" + msg_create_date + ", state=" + state + ", msgcontent="
                + msgcontent + "]";
    }

}
package com.ym.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.ym.entity.Msg;

public class MsgDao extends BaseDao {
    // 1,插入邮件
    public void addMsg(Msg m) {
        Connection con = getConnection();
        String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = con.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(con, 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);
        }

    }
     //(测试删除邮件的代码是否编写成功)
     //    public static void main(String[] args) {
     //    MsgDao md=new MsgDao();
    //    md.delMail(3);
    //}


    // 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("msgid"));
                m.setUsername(rs.getString("username"));
                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) {
            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" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.ym.entity.Msg"%>
<%@ page import="com.ym.dao.MsgDao"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'main.jsp' starting page</title>

</head>

<body bgcolor=#ffccff>
    <%
        String uname = (String) session.getAttribute("uname");
    %>
    欢迎你<%
        out.print(uname);
    %>使用邮箱
    <hr>
    <a href="write.jsp">写邮件</a>
    <table border="1">
        <tr>
            <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><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>"><%=list.get(i).getMsgcontent() %></a>
            </td>
            <td>
                <%
                    if (list.get(i).getState() == 1) {
                %> <img  src="image/unread.png"/>
                <%
                    } else {
                %><img  src="image/read.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>


        </tr>


        <%
            }
        %>

    </table>


</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'write.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="发送"> <br> <a
            href="main.jsp">返回</a>

    </form>

</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.ym.entity.Msg"%>
<%@ page import="com.ym.dao.MsgDao"%>
<%@ page import="com.ym.dao.UsersDao"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'dowrite.jsp' starting page</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 language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.ym.entity.Msg"%>
<%@ page import="com.ym.dao.MsgDao"%>
<%@ page import="com.ym.dao.UsersDao"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'delete.jsp' starting page</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", "2;url=main.jsp");
        //response.sendRedirect("main.jsp");
    %>

</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.ym.entity.Msg"%>
<%@ page import="com.ym.dao.MsgDao"%>
<%@ page import="com.ym.dao.UsersDao"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'detail.jsp' starting page</title>
    
  </head>
  
  <body>
    <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>
  </body>
</html>

 

 

 

 

 

 

搜索

复制

标签:第十二,ps,String,rs,create,JSP,msg,public
来源: https://www.cnblogs.com/boke123456/p/16284342.html