邮件系统

要求 邮件系统 用分层实现 登录 注册  显示邮件列表

 

Msg.java

package bean;

import java.util.Date;

public class Msg {
    // `msgid` int(11) NOT NULL AUTO_INCREMENT,
    // `username` varchar(100) NOT NULL,
    // `title` varchar(20) NOT NULL,
    // `msgcontent` varchar(20) NOT NULL,
    // `state` int(11) NOT NULL,
    // `sendto` varchar(20) NOT NULL,
    // `msg_create_date` datetime NOT NULL,
    private int msgid;
    private String uname;
    private String title;
    private String msgcontent;
    private int state;
    private String sendto;
    private Date msg_create_date;

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

    public Msg() {

    }

    public int getMsgid() {
        return msgid;
    }

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

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    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;
    }

}

Users.java

package bean;

public class Users {
    // username VARCHAR(20) PRIMARY KEY,
    // PASSWORD VARCHAR(20),
    // email VARCHAR(20)

    private String username;
    private String password;
    private String email;
    
    
    public Users() {
        
    }    
    public Users(String username, String password, String email) {
        super();
        this.username = username;
        this.password = password;
        this.email = email;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}

BaseDao.java

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {    
    //获取连接
    protected Connection getConnection(){
        Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                // 2.建立连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/mysql", "root", "123456");
            } catch (Exception e) {
                e.printStackTrace();
            } 
            return conn;
    }    
    
    
    //关闭连接
    protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){        
    try {
        if(rs != null)
            rs.close();
        if(ps != null)
            ps.close();
        if(con != null)
            con.close();
        
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

MsgDao.java

package 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 bean.Msg;

public class MsgDao extends BaseDao {
    // 根据登录用户获取收件列表 收件人=uname
    public List<Msg> getReceive(String uname) {
        List<Msg> list = new ArrayList<Msg>();
        Connection conn = getConnection();
        String sql = "SELECT * from msg where sendto=?";
        PreparedStatement ps;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Msg m=new Msg();//读取一行,创建一个对象
                m.setMsgid(rs.getInt(1));
                m.setUname(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);//添加到list列表中
                
            }
            
            
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return list;

    }
    
    //把邮件对象m写入到数据库
    public void writeMsg(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.getUname());
            ps.setString(2, m.getTitle());
            ps.setString(3, m.getMsgcontent());
            ps.setString(5, m.getSendto());
            ps.setInt(4, 1);//1shi weidu
            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);
        }    
        
    }
    
    //根据消息ID获取消息详情
    public Msg getMsgById(int id){
        Msg m=new Msg();
        Connection conn = getConnection();
        String sql = "SELECT * from msg where msgid=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
             rs=ps.executeQuery();
            while(rs.next()){
                
                m.setMsgid(rs.getInt(1));
                m.setUname(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));                
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, rs);
        }
        
        
        return m;
        
        
        
    }
    
    
    //根据ID删除消息
    public void delMsg(int id){
        Connection con=getConnection();
        PreparedStatement ps=null;
        String sql="delete from msg where msgid=?";
        try {
            ps=con.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(con, ps, null);
        }
        
        
    }
    
    
    //修改邮件的已读状态  把1改0
    public void updateMsg(int id){
        try {
            Connection con = getConnection();
            String sql="update msg set state=0 where msgid=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, id);//给sql语句的问号赋值
            ps.executeUpdate();
            closeAll(con, ps, null);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
    }    
    

}

UsersDao.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bean.Users;

public class UsersDao extends BaseDao {
    
    //登录
    public boolean login(String uname,String upwd){
        boolean f=false;
        Connection conn=getConnection();
        String sql="select * from users where username=? and password=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
             ps=conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            rs=ps.executeQuery();
            if (rs.next()) {
                f=true;
            }            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    finally{
            closeAll(conn, ps, rs);
        }
        return f;
    }    
    //注册,相当于添加一个用户
    public int register(Users u){
        Connection conn=getConnection();
        String sql="insert into users values(?,?,?)";
        PreparedStatement ps=null;
        int i=0;
        try {
             ps=conn.prepareStatement(sql);
            ps.setString(1, u.getUsername());
            ps.setString(2, u.getPassword());
            ps.setString(3, u.getEmail());
            i=ps.executeUpdate();//返回受影响的行数
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    finally{
            closeAll(conn, ps, null);
        }
        return i;
    }
    
    public static void main(String[] args) {
        Users u=new Users("haha", "123456", "haha@163.com");
        UsersDao ud=new UsersDao();
        //System.out.println(ud.register(u));
    System.out.println(ud.login("kit243ty", "777"));
        
    }//修改个人信息

}

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
<form action="dologin.jsp" method="post">
用户名:<input type="text" name="uname" value="ls" /><Br>
密码 :<input type="password" name="upwd" value="456"/><br>

<input type="submit" value="登录">
<a href="register.jsp" >注册</a>

</form>
  </body>
</html>

main.jsp

<%@page import="bean.Msg"%>
<%@page import="dao.MsgDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
    <%
        MsgDao md = new MsgDao();
        String uname = (String) session.getAttribute("uname");
        List<Msg> list = md.getReceive(uname);
    %>
    欢迎你<%=uname%> <a href="write.jsp">写邮件</a>
    <table border="1">
    <tr>
    
    <td>标题    </td>
    
    <td>是否已读</td>
    <td>发件人</td>
    <td>时间</td>    
    <td>操作</td>
    <td>操作</td>
    </tr>
<%for(int i=0;i<list.size();i++){ %>    
    <tr>
    
    <td><a href="readMsg.jsp?mid=<%=list.get(i).getMsgid()%>"><%=list.get(i).getTitle() %></a></td>
    
    <td><%int x=list.get(i).getState();
            if(x==0){%>
            <img src="images/sms_readed.png">
            <%}else{ %>
            <img src="images/sms_unReaded.png">
            <%} %>
    </td>
    <td><%=list.get(i).getUname() %></td>
    <td><%=list.get(i).getMsg_create_date() %></td>
    <td><a href="dodel.jsp?mid=<%=list.get(i).getMsgid()%>">删除</a></td>
    <td><a href="write.jsp?reply=<%=list.get(i).getUname()%>">回复</td>
    
    
    </tr>
    
<%} %>
    
    
    </table>



</body>
</html>

write.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
<form action="dowrite.jsp"  method="post">
收件人:<input type="text" name="sendto" value="<%=request.getParameter("reply") %>" ><br>
主题:<input type="text" name="title"><br>
内容:<textarea name="content"></textarea>
<input type="submit" value="发送">


</form>


  </body>
</html>

 

 

 

 

 

 

 

 

 

posted @ 2021-05-12 00:09  LILY十一啦  阅读(125)  评论(0编辑  收藏  举报