第十三周作业
1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读
public class mail { private Integer id; private String Sender; private String addressee; private String title; private String content; private String time; private Integer state; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSender() { return Sender; } public void setSender(String sender) { Sender = sender; } public String getAddressee() { return addressee; } public void setAddressee(String addressee) { this.addressee = addressee; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } @Override public String toString() { return "mail{" + "id=" + id + ", Sender='" + Sender + '\'' + ", addressee='" + addressee + '\'' + ", title='" + title + '\'' + ", content='" + content + '\'' + ", time='" + time + '\'' + ", state=" + state + '}'; } }
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(); } } }
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class mail_UserDao extends mail_BaseDao{ public boolean login(String name, String pwd) { boolean f = false; Connection conn = getConnection(); String sql = "select * from tb_user where username=? and password=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); rs = ps.executeQuery(); if (rs.next()) f = true; } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(conn, ps, rs); } return f; } public void reg(String username, String password) { Connection conn = getConnection(); PreparedStatement ps = null; try { String sql = "insert into tb_user(username,password) values(?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, username); ps.setString(2, password); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(conn, ps, null); } } }
import com.own.mywebdemo.Week5_Login_connecor.BaseDao; import org.jetbrains.annotations.NotNull; 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 MailDao extends mail_BaseDao { public List<mail> getMailByReceiver(String name){ List<mail> list=new ArrayList<mail>(); Connection conn=getConnection(); String sql="select * from tb_mail where addressee=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps = conn.prepareStatement(sql); ps.setString(1, name); rs=ps.executeQuery(); while(rs.next()){ mail m=new mail(); m.setId(rs.getInt(1)); m.setSender(rs.getString(2)); m.setAddressee(rs.getString(3)); m.setTitle(rs.getString(4)); m.setContent(rs.getString(5)); m.setTime(rs.getString(6)); m.setState(rs.getInt(7)); list.add(m); } } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(conn, ps, rs); } return list; } // 插入邮件 public void addMail(mail m) { Connection conn = getConnection(); String sql = "insert into tb_mail(Sender,addressee,title,content,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, 0); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(conn, ps, null); } } // 删除邮件 public void deleteMail(int id) { Connection conn = getConnection(); String sql = "delete from tb_mail where id=?"; PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(conn, ps, null); } } public void updataMail(int id){ Connection conn = getConnection(); String sql = "update tb_mail set state=1 where id=?"; PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(conn, ps, null); } } }
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 + '\'' + '}'; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String account=request.getParameter("account"); String psw=request.getParameter("psw"); mail_UserDao mail_userDao = new mail_UserDao(); if (mail_userDao.login(account,psw)){ session.setAttribute("account",account); request.getRequestDispatcher("LogSuccess.jsp").forward(request,response); }else { request.getRequestDispatcher("LogFail.jsp").forward(request,response); } %> </body> </html>