第十二周作业
1 package com.sk.dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class BaseDao { 10 public Connection conn = null; 11 PreparedStatement ps = null; 12 ResultSet rs = null; 13 14 //获取连接 15 protected Connection getConnection(){ 16 17 try { 18 //加载驱动 19 Class.forName("com.mysql.jdbc.Driver"); 20 //建立连接 21 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); 22 } catch (Exception e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 return conn; 27 } 28 29 //关闭连接 30 protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs){ 31 try{ 32 if(rs != null) 33 rs.close(); 34 if(ps != null) 35 ps.close(); 36 if(con != null) 37 con.close(); 38 }catch (SQLException e) { 39 e.printStackTrace(); 40 } 41 } 42 } BaseDao
1 package com.sk.dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class BaseDao { 10 public Connection conn = null; 11 PreparedStatement ps = null; 12 ResultSet rs = null; 13 14 //获取连接 15 protected Connection getConnection(){ 16 17 try { 18 //加载驱动 19 Class.forName("com.mysql.jdbc.Driver"); 20 //建立连接 21 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); 22 } catch (Exception e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 return conn; 27 } 28 29 //关闭连接 30 protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs){ 31 try{ 32 if(rs != null) 33 rs.close(); 34 if(ps != null) 35 ps.close(); 36 if(con != null) 37 con.close(); 38 }catch (SQLException e) { 39 e.printStackTrace(); 40 } 41 } 42 } BaseDao
1 package com.sk.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.Date; 9 import java.util.List; 10 11 import com.sk.entity.Msg; 12 13 public class MsgDao extends BaseDao { 14 //关于邮件的增删改查 15 public static void main(String[] args) { 16 MsgDao md = new MsgDao(); 17 Msg m= md.details(11); 18 19 System.out.println(m.getSendto()); 20 } 21 //添加邮件(写邮件、回复邮件都调用,邮件状态为未读=0,时间为系统时间) 22 public void addMsg(Msg m){ 23 Connection conn = getConnection(); 24 String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) value(?,?,?,0,?,?)"; 25 PreparedStatement ps = null; 26 try { 27 ps = conn.prepareStatement(sql); 28 ps.setString(1, m.getUsername()); 29 ps.setString(2, m.getTitle()); 30 ps.setString(3, m.getMsgcontent()); 31 ps.setString(4, m.getSendto()); 32 ps.setDate(5, new java.sql.Date(new Date().getTime()));//获取系统当前时间 33 ps.executeUpdate(); 34 } catch (SQLException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 }finally{ 38 closeAll(conn, ps, null); 39 } 40 } 41 42 //删除邮件(根据id) 43 public void delMsg(int id){ 44 Connection con = getConnection(); 45 String sql = "delete from msg where msgid=" + id; 46 PreparedStatement ps = null; 47 try { 48 ps = con.prepareStatement(sql); 49 ps.executeUpdate(); 50 } catch (SQLException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 }finally{ 54 closeAll(con, ps, null); 55 } 56 } 57 //查看邮件详情 修改邮件状态(根据id 已读 = 1) 58 public void updateState(int msgid){ 59 Connection con = getConnection(); 60 String sql = "update msg set state=1 where msgid=?"; 61 PreparedStatement ps = null; 62 try { 63 ps = con.prepareStatement(sql); 64 ps.setInt(1, msgid); 65 ps.executeUpdate(); 66 } catch (SQLException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 }finally{ 70 closeAll(con, ps, null); 71 } 72 } 73 public Msg details(int msgid){ 74 Msg m = new Msg(); 75 Connection con = getConnection(); 76 String sql = "select * from msg where msgid=?"; 77 PreparedStatement ps = null; 78 ResultSet rs = null; 79 try { 80 ps = con.prepareStatement(sql); 81 ps.setInt(1, msgid); 82 rs = ps.executeQuery(); 83 while(rs.next()){ 84 m.setMsgid(rs.getInt(1)); 85 m.setUsername(rs.getString(2)); 86 m.setTitle(rs.getString(3)); 87 m.setMsgcontent(rs.getString(4)); 88 m.setState(rs.getInt(5)); 89 m.setSendto(rs.getString(6)); 90 m.setMsg_create_date(rs.getDate(7)); 91 } 92 } catch (SQLException e) { 93 // TODO Auto-generated catch block 94 e.printStackTrace(); 95 }finally{ 96 closeAll(con, ps, rs); 97 } 98 return m; 99 } 100 //查看邮件(根据 收件人username) 101 public List<Msg> getMailByReceiver(String uname){ 102 List<Msg> list = new ArrayList<Msg>(); 103 Connection con = getConnection(); 104 String sql = "select * from msg where username=?"; 105 PreparedStatement ps = null; 106 ResultSet rs = null; 107 try { 108 ps = con.prepareStatement(sql); 109 ps.setString(1, uname); 110 rs = ps.executeQuery(); 111 while(rs.next()){ 112 //每读取一行,创建一个msg对象,对象放在list中 113 Msg m = new Msg(); 114 m.setMsgid(rs.getInt(1)); 115 m.setUsername(rs.getString(2)); 116 m.setTitle(rs.getString(3)); 117 m.setMsgcontent(rs.getString(4)); 118 m.setState(rs.getInt(5)); 119 m.setSendto(rs.getString(6)); 120 m.setMsg_create_date(rs.getDate(7)); 121 list.add(m); 122 } 123 } catch (SQLException e) { 124 // TODO Auto-generated catch block 125 e.printStackTrace(); 126 }finally{ 127 closeAll(con, ps, rs); 128 } 129 130 return list; 131 } 132 133 //查看未读邮件(根据 收件人username) 134 public List<Msg> getUnReadMailByReceiver(String uname){ 135 List<Msg> list = new ArrayList<Msg>(); 136 Connection con = getConnection(); 137 String sql = "select * from msg where username=? and state=0"; 138 PreparedStatement ps = null; 139 ResultSet rs = null; 140 try { 141 ps = con.prepareStatement(sql); 142 ps.setString(1, uname); 143 rs = ps.executeQuery(); 144 while(rs.next()){ 145 //每读取一行,创建一个msg对象,对象放在list中 146 Msg m = new Msg(); 147 m.setMsgid(rs.getInt(1)); 148 m.setUsername(rs.getString(2)); 149 m.setTitle(rs.getString(3)); 150 m.setMsgcontent(rs.getString(4)); 151 m.setState(rs.getInt(5)); 152 m.setSendto(rs.getString(6)); 153 m.setMsg_create_date(rs.getDate(7)); 154 list.add(m); 155 } 156 } catch (SQLException e) { 157 // TODO Auto-generated catch block 158 e.printStackTrace(); 159 }finally{ 160 closeAll(con, ps, rs); 161 } 162 163 return list; 164 } 165 } MsgDao
1 package com.sk.entity; 2 3 public class Users { 4 private int id; 5 private String uname; 6 private String upwd; 7 8 9 public Users() { 10 super(); 11 } 12 public Users(String uname, String upwd) { 13 super(); 14 this.uname = uname; 15 this.upwd = upwd; 16 } 17 public int getId() { 18 return id; 19 } 20 public void setId(int id) { 21 this.id = id; 22 } 23 public String getUname() { 24 return uname; 25 } 26 public void setUname(String uname) { 27 this.uname = uname; 28 } 29 public String getUpwd() { 30 return upwd; 31 } 32 public void setUpwd(String upwd) { 33 this.upwd = upwd; 34 } 35 36 37 } Users
1 package com.sk.entity; 2 3 import java.util.Date; 4 5 public class Msg { 6 private int msgid; 7 private String username; 8 private String title; 9 private String msgcontent; 10 private int state; 11 private String sendto; 12 private Date msg_create_date; 13 14 public int getMsgid() { 15 return msgid; 16 } 17 public void setMsgid(int msgid) { 18 this.msgid = msgid; 19 } 20 public String getUsername() { 21 return username; 22 } 23 public void setUsername(String username) { 24 this.username = username; 25 } 26 public String getTitle() { 27 return title; 28 } 29 public void setTitle(String title) { 30 this.title = title; 31 } 32 public String getMsgcontent() { 33 return msgcontent; 34 } 35 public void setMsgcontent(String msgcontent) { 36 this.msgcontent = msgcontent; 37 } 38 public int getState() { 39 return state; 40 } 41 public void setState(int state) { 42 this.state = state; 43 } 44 public String getSendto() { 45 return sendto; 46 } 47 public void setSendto(String sendto) { 48 this.sendto = sendto; 49 } 50 public Date getMsg_create_date() { 51 return msg_create_date; 52 } 53 public void setMsg_create_date(Date msg_create_date) { 54 this.msg_create_date = msg_create_date; 55 } 56 57 } Msg
1 <%@page import="com.sk.dao.MsgDao"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 3 <% 4 request.setCharacterEncoding("UTF-8"); 5 6 int id = Integer.parseInt(request.getParameter("id")); 7 MsgDao md = new MsgDao(); 8 md.delMsg(id); 9 10 response.sendRedirect("main.jsp"); 11 %> del.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>send</title> 6 </head> 7 <body> 8 <div align="center"> 9 <h3>编写你要发送的邮件</h3> 10 <h3><a href="main.jsp" >返回上一页</a></h3> 11 <form action="dosend.jsp" method="post"> 12 <table> 13 <tr> 14 <td>收件人:</td> 15 <td><input type="text" name="username" value="<%=request.getParameter("reply")%>"></td> 16 </tr> 17 <tr> 18 <td>标题:</td> 19 <td><input type="text" name="title"></td> 20 </tr> 21 <tr> 22 <td>内容:</td> 23 <td><textarea rows="6" cols="18" name="msgcontent"></textarea></td> 24 </tr> 25 </table> 26 <table> 27 <tr> 28 <td><input type="submit" value="提交"> </td> 29 <td> </td> 30 <td><input type="reset" value="重置"></td> 31 </tr> 32 </table> 33 </form> 34 </div> 35 </body> 36 </html> send.jsp
1 <%@page import="com.sk.dao.MsgDao"%> 2 <%@page import="com.sk.entity.Msg"%> 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 4 <% 5 request.setCharacterEncoding("UTF-8"); 6 7 String uname = (String)session.getAttribute("uname");//发件人在session中获取 8 String title = request.getParameter("title"); 9 String username = request.getParameter("username"); 10 String msgcontent = request.getParameter("msgcontent"); 11 12 Msg m = new Msg(); 13 m.setTitle(title); 14 m.setMsgcontent(msgcontent); 15 m.setSendto(uname); 16 m.setUsername(username); 17 18 MsgDao md = new MsgDao(); 19 md.addMsg(m); 20 21 out.print("发送成功,即将跳回首页......"); 22 response.setHeader("refresh", "3;url=main.jsp"); 23 24 %> dosend.jsp