第十二周作业

1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读
package com.sf.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 javax.annotation.PreDestroy;

import com.sf.entity.msg;
import com.sun.org.apache.regexp.internal.recompile;

public class msgDao extends BaseDao{
    
    public List<msg> listAll(String sendto) throws SQLException{
        List<msg> list =new ArrayList<msg>();
        Connection conn =getConnection();
        String sql="select * from msg where sendto=?";
        PreparedStatement pred =null;
        
        ResultSet rest=null;
        try {
            pred=conn.prepareStatement(sql);
            pred.setString(1, sendto);
            rest=pred.executeQuery();
            while (rest.next()) {
                msg msg=new msg();
                msg.setMsgid(rest.getInt("msgid"));
                msg.setUsername(rest.getString("username"));
                msg.setTitle(rest.getString("title"));
                msg.setMsgcontent(rest.getString("msgcontent"));
                msg.setState(rest.getInt("state"));
                msg.setSendto(rest.getString("sendto"));
                msg.setMsg_create_date(rest.getDate("msg_create_date"));
                list.add(msg);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        finally{
            closeAll(conn, pred, rest);
        }
        return list;
    }
    
    public void changestate(Integer msgid){
        Connection conn =getConnection();
        
        String sql="update msg set state=1 where msgid=?";
        PreparedStatement pred=null;

        try {

            pred =conn.prepareStatement(sql);
            pred.setInt(1, msgid);
            pred.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            closeAll(conn, pred, null);
        }
    }
        public List<msg> showContent(Integer msgid) {
            List<msg> list=new ArrayList<msg>();
            Connection conn=getConnection();
            String sql="select * from msg where msgid=?";
            PreparedStatement pred=null;
            ResultSet rest=null;
            try {
                pred = conn.prepareStatement(sql);
                pred.setInt(1, msgid);
                rest =pred.executeQuery();
                while(rest.next()){
                    msg msg =new msg();
                    msg.setUsername(rest.getString("username"));
                    msg.setTitle(rest.getString("title"));
                    msg.setMsgcontent(rest.getString("msgcontent"));
                    msg.setMsg_create_date(rest.getDate("msg_create_date"));
                    list.add(msg);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                closeAll(conn, pred, rest);
            }
            return list;
        }
        public void delmsg(Integer msgid){
            Connection conn =getConnection();
            
            String sql="delete from msg where msgid=?";
            PreparedStatement pred=null;

            try {

                pred =conn.prepareStatement(sql);
                pred.setInt(1, msgid);
                pred.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                closeAll(conn, pred, null);
            }
        }
        
        public void send(msg msg){
            Connection conn=getConnection();
            String sql="insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,0,?,?)";
            PreparedStatement ps=null;
            try {
                 ps=conn.prepareStatement(sql);
                 ps.setString(1, msg.getUsername());
                 ps.setString(2, msg.getTitle());
                 ps.setString(3, msg.getMsgcontent());
                 ps.setString(4, msg.getSendto());
                 ps.setDate(5, new java.sql.Date(new Date().getTime()));
                 ps.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                closeAll(conn, ps, null);
            }        
        }
}
<%@page import="com.sf.entity.msg"%>
<%@page import="com.sf.dao.msgDao"%>
<%@page import="sun.security.jgss.LoginConfigImpl"%>
<%@page import="com.sf.dao.usersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

  <body>
          <%
              String msgid1=request.getParameter("msgid");
              Integer msgid=Integer.parseInt(msgid1);
              msgDao msgDao =new msgDao();
              msgDao.delmsg(msgid);
              response.sendRedirect("zhuye.jsp");
           %>
  </body>
</html>
<%@page import="sun.security.jgss.LoginConfigImpl"%>
<%@page import="com.sf.dao.usersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

  <body>
          <%
              String uname = request.getParameter("uname");
              String upwd =request.getParameter("upwd");
              String uid =request.getParameter("uid");
              Integer id =Integer.parseInt(uid);
              
              usersDao usersDao=new usersDao();
              if(usersDao.login(uname, upwd, id)){
                  session.setAttribute("uname", uname);
                  request.getRequestDispatcher("zhuye.jsp").forward(request, response);
              }
              else{
                  out.print("登录失败,5s后跳转登录页面");
                  response.setHeader("refresh", "5;url=login.jsp");
              }
           %>
  </body>
</html>
<%@page import="com.sf.entity.msg"%>
<%@page import="com.sf.dao.msgDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

  <body>
          <%
              String magid =request.getParameter("msgid");
              Integer msgid =Integer.parseInt(magid);
              
              
              msgDao msgDao =new msgDao();
              msgDao.changestate(msgid);
              msg msg=new msg();
              List<msg> list =new ArrayList<msg>();
              list=msgDao.showContent(msgid);
              %>
              <table width="600px" border="2px solid red">
          
                   <tr>
                       <th>发件人</th><th>标题</th><th>内容</th><th>发送时间</th>
                   </tr>
                   <%
                   
                    for(msg msg1:list){
                %>
                   <tr>
                       <td><%out.print(msg1.getUsername()); %></td>
                       <td><%out.print(msg1.getTitle()); %></td>
                       <td><%out.print(msg1.getMsgcontent()); %></td>
                       <td><%out.print(msg1.getMsg_create_date()); %></td>
                   </tr>
                   <%} %>
           </table>
           <a href="zhuye.jsp">继续浏览</a>
  </body>
</html>
<%@page import="com.sf.entity.msg"%>
<%@page import="com.sf.dao.msgDao"%>
<%@page import="sun.security.jgss.LoginConfigImpl"%>
<%@page import="com.sf.dao.usersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

  <body>
          <%
              String uname=(String)session.getAttribute("uname");
              String sendto = request.getParameter("sendto");
              String title =request.getParameter("title");
              String content =request.getParameter("content");
                  msg msg =new msg();
                msg.setTitle(title);
                msg.setMsgcontent(content);
                msg.setUsername(uname);
                msg.setSendto(sendto);
                
                  msgDao msgDao=new msgDao();
                  msgDao.send(msg);
                  out.print("发送成功,即将跳回主页.....");
                response.setHeader("refresh", "5;url=zhuye.jsp");
           %>
           
  </body>
</html>
<%@page import="com.sf.dao.usersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

  <body>
          <%    
              String uid=request.getParameter("id");
              Integer id=Integer.parseInt(uid);
              String uname=request.getParameter("uname");
              String upwd =request.getParameter("upwd");
              String upwd2 =request.getParameter("upwd2");
              usersDao usersDao=new usersDao();
              if(upwd.equals(upwd2)){
                  if(usersDao.id(id)){
                      out.print("已经有这个id了,即将跳回注册页.....");
                    response.setHeader("refresh", "5;url=zhuce.jsp");
                  }
                  else{
                      usersDao.zhuce(id, uname, upwd);
                      out.print("注册成功,即将跳回登录页.....");
                    response.setHeader("refresh", "5;url=login.jsp");
                }
              }
              else{
                out.print("两次密码不一致,即将跳回注册页.....");
                response.setHeader("refresh", "5;url=zhuce.jsp");
    }
           %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

  <body>
          <script type="text/javascript">
              function dologin(){
                  if(loginform.id.value==""){
                      alert("没有输入id");
                      return;
                  }
                  if(loginform.uname.value==""){
                      alert("没有输入用户名");
                      return;
                  }
                  if(loginform.upwd.value==""){
                      alert("没有输入密码");
                      return;
                  }
                  loginform.submit();
              }
          </script>
      <form action="dologin.jsp" name="loginform" method="post">
          id:<input type="number" value="1" name="uid"/></br>
          用户名:<input type="text" value="zs" name="uname"/></br>
          密码:<input type="password" value="11" name="upwd"/></br>
          <input type="button" value="登录" onclick="dologin()"/>
          <a href="zhuce.jsp">没有账号?立即注册</a>
          </form>
  </body>
</html>
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();
        }
         
         
         
    }   
     
 
}

 

posted @ 2022-05-21 22:08  五谷鸡爪🥳  阅读(2)  评论(0编辑  收藏  举报