1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现
package doservlet;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import dao.MsgDao;
 
/**
 * Servlet implementation class dodelete
 */
@WebServlet("/dodelete")
public class dodelete extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dodelete() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int id=Integer.parseInt(request.getParameter("id"));
        MsgDao md=new MsgDao();
        md.delMail(id);
        response.sendRedirect("main.jsp");
    }
 
}

  

public class MailUsers {
    private int id;
    private String username;
    private String password;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    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;
    }
 
    @Override
    public String toString() {
        return "MailUsers{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

  

import java.sql.*;
 
public class mail_BaseDao {
    protected Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/mailbox", "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();
        }
    }
}

  

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(?,?,?,1,?,?)";
        PreparedStatement ps=null;
        try {
             ps=conn.prepareStatement(sql);
             ps.setString(1, m.getUsername());
             ps.setString(2, m.getTitle());
             ps.setString(3, m.getMsgcontent());
             ps.setString(4, m.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);
        }        
    }
    public static void main(String[] args) {
        msgDao md=new msgDao();
        md.delMsg(4);
    }    
    
    //根据id删除邮件
    public void delMsg(int id){
        Connection con=getConnection();
        String sql="delete from msg where msgid="+id;
        PreparedStatement ps=null;
        try {
            ps=con.prepareStatement(sql);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(con, ps, null);
        }
        
    }
    
    
    
    
    //根据id修改邮件状态(统一改成1)
    
    
    //根据收件人查看全部邮件
    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对象,对象放到集合中
                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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }        
        return list;
    }
    
    
    
    

}

  

package com.gd.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gd.entity.users;
public class usersDao extends BaseDao{
//关于用户的增删改查
    public boolean denglu(String uname,String upwd){
        boolean f=false;
        Connection conn=getConnection();
        String sql=" select * from users where unmae=? and upwd=?";
        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: handle exception
            e.printStackTrace();
        } finally{
            closeAll(conn,ps,rs);
        }
        return f;                
    }
    
    public void addUser(users u){
        
        
        
    }
}

  

posted on 2022-05-28 23:03  王运聪  阅读(12)  评论(0编辑  收藏  举报