jsp第十二次作业
package com.entity; import java.util.Date; public class Email { private Integer id; private String fa; private String shou; private String title; private Date time; private String zhuang; private String nei; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getShou() { return shou; } public void setShou(String shou) { this.shou = shou; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } public String getZhuang() { return zhuang; } public void setZhuang(String zhuang) { this.zhuang = zhuang; } public String getNei() { return nei; } public void setNei(String nei) { this.nei = nei; } @Override public String toString() { return "Email [id=" + id + ", fa=" + fa + ", shou=" + shou + ", title=" + title + ", time=" + time + ", zhuang=" + zhuang + ", nei=" + nei + "]"; } public Email(Integer id, String fa, String shou, String title, Date time, String zhuang, String nei) { super(); this.id = id; this.fa = fa; this.shou = shou; this.title = title; this.time = time; this.zhuang = zhuang; this.nei = nei; } public Email() { super(); } }
package com.entity; public class User { private Integer id; private String uname; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User() { super(); } public User(Integer id, String uname, String password) { super(); this.id = id; this.uname = uname; this.password = password; } }
package com.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { //连接数据库 public Connection getConnection() { Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); } catch (Exception e) { e.printStackTrace(); } return con; } //关闭数据库 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.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.entity.Email; public class EmailDao extends BaseDao{ public List<Email> getAll(String uname){ List<Email>list=new ArrayList<Email>(); Connection con=getConnection(); PreparedStatement pred=null; ResultSet resultSet=null; String sql="select * from email where shou=?"; try { pred=con.prepareStatement(sql); pred.setString(1, uname); resultSet=pred.executeQuery(); while(resultSet.next()){ Email email=new Email(); email.setId(resultSet.getInt(1)); email.setFa(resultSet.getString(2)); email.setShou(resultSet.getString(3)); email.setTitle(resultSet.getString(4)); email.setNei(resultSet.getString(5)); email.setTime(resultSet.getDate(6)); email.setZhuang(resultSet.getString(7)); list.add(email); } } catch (SQLException e) { e.printStackTrace(); } return list; } public void addEmail(Email e) { Connection con = getConnection(); String sql = "insert into email(fa,shou,title,nei,time,zhuang) values(?,?,?,?,?,'0')"; PreparedStatement pred = null; try { pred = con.prepareStatement(sql); pred.setString(1, e.getFa()); pred.setString(2, e.getShou()); pred.setString(3, e.getTitle()); pred.setString(4, e.getNei()); pred.setDate(5, new java.sql.Date(new Date().getTime())); pred.executeUpdate(); } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, null); } } public void del(int id) { Connection con = getConnection(); String sql = "delete from email where id=?"; PreparedStatement pred = null; try { pred = con.prepareStatement(sql); pred.setInt(1, id); pred.executeUpdate(); } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, null); } } public void update(int id) { Connection con = getConnection(); String sql = "update email set zhuang='1' where id=?"; PreparedStatement pred = null; try { pred = con.prepareStatement(sql); pred.setInt(1, id); pred.executeUpdate(); } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, null); } } public Email look(int id) { Connection con = getConnection(); String sql = "select id,fa,shou,title,nei,time from email where id=?"; PreparedStatement pred = null; ResultSet resultSet = null; try { pred = con.prepareStatement(sql); pred.setInt(1, id); resultSet = pred.executeQuery(); while (resultSet.next()) { Email email = new Email(); email.setId(resultSet.getInt(1)); email.setFa(resultSet.getString(2)); email.setShou(resultSet.getString(3)); email.setTitle(resultSet.getString(4)); email.setNei(resultSet.getString(5)); email.setTime(resultSet.getDate(6)); return email; } } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, resultSet); } return null; } } package com.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.entity.Email; public class EmailDao extends BaseDao{ public List<Email> getAll(String uname){ List<Email>list=new ArrayList<Email>(); Connection con=getConnection(); PreparedStatement pred=null; ResultSet resultSet=null; String sql="select * from email where shou=?"; try { pred=con.prepareStatement(sql); pred.setString(1, uname); resultSet=pred.executeQuery(); while(resultSet.next()){ Email email=new Email(); email.setId(resultSet.getInt(1)); email.setFa(resultSet.getString(2)); email.setShou(resultSet.getString(3)); email.setTitle(resultSet.getString(4)); email.setNei(resultSet.getString(5)); email.setTime(resultSet.getDate(6)); email.setZhuang(resultSet.getString(7)); list.add(email); } } catch (SQLException e) { e.printStackTrace(); } return list; } public void addEmail(Email e) { Connection con = getConnection(); String sql = "insert into email(fa,shou,title,nei,time,zhuang) values(?,?,?,?,?,'0')"; PreparedStatement pred = null; try { pred = con.prepareStatement(sql); pred.setString(1, e.getFa()); pred.setString(2, e.getShou()); pred.setString(3, e.getTitle()); pred.setString(4, e.getNei()); pred.setDate(5, new java.sql.Date(new Date().getTime())); pred.executeUpdate(); } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, null); } } public void del(int id) { Connection con = getConnection(); String sql = "delete from email where id=?"; PreparedStatement pred = null; try { pred = con.prepareStatement(sql); pred.setInt(1, id); pred.executeUpdate(); } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, null); } } public void update(int id) { Connection con = getConnection(); String sql = "update email set zhuang='1' where id=?"; PreparedStatement pred = null; try { pred = con.prepareStatement(sql); pred.setInt(1, id); pred.executeUpdate(); } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, null); } } public Email look(int id) { Connection con = getConnection(); String sql = "select id,fa,shou,title,nei,time from email where id=?"; PreparedStatement pred = null; ResultSet resultSet = null; try { pred = con.prepareStatement(sql); pred.setInt(1, id); resultSet = pred.executeQuery(); while (resultSet.next()) { Email email = new Email(); email.setId(resultSet.getInt(1)); email.setFa(resultSet.getString(2)); email.setShou(resultSet.getString(3)); email.setTitle(resultSet.getString(4)); email.setNei(resultSet.getString(5)); email.setTime(resultSet.getDate(6)); return email; } } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, resultSet); } return null; } }
package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UserDao extends BaseDao{ public int Register(String uname,String password){ int i=-1; PreparedStatement pred=null; Connection con=getConnection(); String sql="insert into user(uname,password)values(?,?)"; try { pred= con.prepareStatement(sql); pred.setString(1, uname); pred.setString(2, password); i=pred.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(con, pred, null); } return i; } public boolean Login(String uname,String password){ boolean f=false; PreparedStatement pred=null; ResultSet resultSet=null; Connection con=getConnection(); String sql="select * from user where uname=? and password=?"; try { pred=con.prepareStatement(sql); pred.setString(1, uname); pred.setString(2, password); resultSet=pred.executeQuery(); while(resultSet.next()){ f=true; } } catch (SQLException e) { e.printStackTrace(); } return f; } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.ym.entity.Msg"%> <%@ page import="com.ym.dao.MsgDao"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'main.jsp' starting page</title> </head> <body bgcolor=#ffccff> <% String uname = (String) session.getAttribute("uname"); %> 欢迎你<% out.print(uname); %>使用邮箱 <hr> <a href="write.jsp">写邮件</a> <table border="1"> <tr> <td>发件人</td> <td>标题</td> <td>状态</td> <td>时间</td> <td>操作</td> <td>操作</td> <% MsgDao md = new MsgDao(); List<Msg> list = md.getMailByReceiver(uname); for (int i = 0; i < list.size(); i++) { %> <tr> <td><%=list.get(i).getUsername()%></td> <td><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>"><%=list.get(i).getMsgcontent() %></a> </td> <td> <% if (list.get(i).getState() == 1) { %> <img src="image/unread.png"/> <% } else { %><img src="image/read.png"/> <% } %> </td> <td><%=list.get(i).getMsg_create_date()%></td> <td><a href="delete.jsp?id=<%=list.get(i).getMsgid()%>">删除</a> </td> <td><a href="write.jsp?reply=<%=list.get(i).getUsername()%>">回复</a> </td> </tr> <% } %> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'write.jsp' starting page</title> </head> <body> <form action="dowrite.jsp" method="post"> 收件人:<input type="text" name="receiver" value="<%=request.getParameter("reply")%>"><br> 主题: <input type="text" name="title"><br> 内容 <textarea rows="6" cols="20" name="content"></textarea> <br> <input type="submit" value="发送"> <br> <a href="main.jsp">返回</a> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.ym.entity.Msg"%> <%@ page import="com.ym.dao.MsgDao"%> <%@ page import="com.ym.dao.UsersDao"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'dowrite.jsp' starting page</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = (String) session.getAttribute("uname"); String sendto = request.getParameter("receiver"); String title = request.getParameter("title"); String content = request.getParameter("content"); Msg m = new Msg(); m.setMsgcontent(content); m.setUsername(uname); m.setSendto(sendto); m.setTitle(title); MsgDao md = new MsgDao(); md.addMsg(m); out.print("发送成功....."); response.setHeader("refresh", "3;url=main.jsp"); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.ym.entity.Msg"%> <%@ page import="com.ym.dao.MsgDao"%> <%@ page import="com.ym.dao.UsersDao"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'delete.jsp' starting page</title> </head> <body> <% request.setCharacterEncoding("utf-8"); int id = Integer.parseInt(request.getParameter("id")); MsgDao md = new MsgDao(); md.delMail(id); out.print("刪除成功......"); response.setHeader("refresh", "2;url=main.jsp"); //response.sendRedirect("main.jsp"); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.ym.entity.Msg"%> <%@ page import="com.ym.dao.MsgDao"%> <%@ page import="com.ym.dao.UsersDao"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'detail.jsp' starting page</title> </head> <body> <body> <% request.setCharacterEncoding("utf-8"); String msgid = request.getParameter("id"); int idd = Integer.parseInt(msgid); MsgDao md = new MsgDao(); md.update(idd); Msg m =md.read(idd); %> <table> <tr> <td>发件人:</td> <td><input type="text" name="username" style="border: none" value="<%=m.getUsername()%>"> </td> </tr> <tr> <td>主题:</td> <td><input type="text" name="title" style="border: none" value="<%=m.getTitle()%>"> </td> </tr> <tr> <td>时间:</td> <td><input type="text" name="msg_create_date" style="border: none" value="<%=m.getMsg_create_date()%>"> </td> </tr> <tr> <td>收件人:</td> <td><input type="text" name="sendto" style="border: none" value="<%=m.getSendto()%>"> </td> </tr> <tr> <td>内容:</td> <td><div style="border: none;outline: none;overflow: inherit;"> <%=m.getMsgcontent()%></div> </td> </tr> </table> <br> <a href="main.jsp">返回</a> </body> </body> </html>