第十三次作业

package dao;
 
import java.sql.*;
 
public class BaseDao {
    //获取连接
    protected Connection getConnection(){
        Connection conn=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2.建立连接
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost/mysqldb?useSSL=true&useUnicode=true&characterEncoding=utf8", "root", "root");
        } 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 dao;
 
import util.DBUtil;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class UserDao extends BaseDao{
    public boolean login(String name,String pwd){
        boolean f=false;
        Connection conn= DBUtil.getCon();
        String sql="select * from users where uname=? and upwd=?";
        PreparedStatement ps;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            ResultSet rs=ps.executeQuery();
            if(rs.next())
                f=true;
            closeAll(conn, ps, rs);
 
 
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return f;
    }
    public int reg(String uname, String upwd) {
        Connection conn = getConnection();
        int i=0;
        PreparedStatement ps = null;
        try {
            String sql = "insert into users(uname,upwd) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            i=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
        return i;
    }
}
package dao;
 
import pojo.Msg;
import util.DBUtil;
 
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;
 
public class MsgDao extends BaseDao{
    public List<Msg> getMailByReceiver(String name) {
        List<Msg> list = new ArrayList<Msg>();
        Connection conn = getConnection();
        String sql = "select * from mail where addressee=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            while (rs.next()) {
                Msg m = new Msg();
                int eid = rs.getInt("eid");
                String sender = rs.getString("sender");
                String addressee = rs.getString("addressee");
                String title = rs.getString("title");
                String content = rs.getString("content");
                Date sending_time = rs.getDate("sending_time");
                int state = rs.getInt("state");
                m.setEid(eid);
                m.setSender(sender);
                m.setAddressee(addressee);
                m.setTitle(title);
                m.setContent(content);
                m.setSending_time(sending_time);
                m.setState(state);
                list.add(m);
            }
 
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return list;
    }
 
    public void addMsg(Msg m) {
        Connection conn = getConnection();
        String sql = "insert into mail(sender,addressee,title,content,sending_time,state) values(?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, m.getSender());
            ps.setString(2, m.getAddressee());
            ps.setString(3, m.getTitle());
            ps.setString(4, m.getContent());
            ps.setDate(5, new java.sql.Date(new Date().getTime()));
            ps.setInt(6, m.getState());
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
 
    }
 
    public void delMail(int id) {
        Connection conn = getConnection();
        String sql = "delete from mail where eid=?";
        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 Msg getMailById(int id) {
        Connection conn = getConnection();
        String sql = "select * from mail where eid=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        Msg m = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                m = new Msg();
                int eid = rs.getInt("eid");
                String sender = rs.getString("sender");
                String addressee = rs.getString("addressee");
                String title = rs.getString("title");
                String content = rs.getString("content");
                Date sending_time = rs.getDate("sending_time");
 
                m.setEid(eid);
                m.setSender(sender);
                m.setAddressee(addressee);
                m.setTitle(title);
                m.setContent(content);
                m.setSending_time(sending_time);
                m.setState(readMail(id) == 1 ? 0 : 1);
            }
 
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return m;
    }
 
    public int readMail(int id) {
        int n = 0;
        Connection conn = getConnection();
        String sql = "update mail set state='0' where eid=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            n = ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
        return n;
    }
}
package service; 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.UserDao; /** * Servlet implementation class doreg */ @WebServlet("/doreg") public class doreg extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public doreg() { 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 String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); UserDao ud=new UserDao(); ud.reg(uname, upwd); request.getRequestDispatcher("login.jsp").forward(request, response); } }
复制代码
复制代码
package service;
 
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.UserDao;
 
 
/**
 * Servlet implementation class dologin
 */
@WebServlet("/dologin")
public class dologin extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dologin() {
        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
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UserDao ud=new UserDao();
        if(ud.login(uname, upwd)){
            request.getSession().setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(request, response);
        }else{
            response.getWriter().print("登陆失败,即将跳回登陆页.....");
            response.setHeader("refresh", "5;url=login.jsp");
        }
    }
 
}
复制代码
复制代码
package service;
 
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");
    }
 
}
复制代码
复制代码
package service;
 
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;
import pojo.Msg;
 
/**
 * Servlet implementation class dowrite
 */
@WebServlet("/dowrite")
public class dowrite extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dowrite() {
        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
        request.setCharacterEncoding("utf-8");
        String uname=(String)request.getSession().getAttribute("uname");
        String addressee=request.getParameter("addressee");
        String title=request.getParameter("title");
        String content=request.getParameter("content");
 
        Msg m=new Msg();
        m.setContent(content);
        m.setSender(uname);
        m.setAddressee(addressee);
        m.setTitle(title);
 
        MsgDao md=new MsgDao();
        md.addMsg(m);
 
        response.getWriter().print("发送成功.....");
        response.setHeader("refresh", "5;url=main.jsp");
    }
 
}

 

posted @ 2022-05-29 21:50  计算机1901王怡  阅读(7)  评论(0编辑  收藏  举报