第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
package com.gd.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class BaseDao { //连接数据库 public Connection getConnection(){ Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ttest?characterEncoding=utf-8","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 (Exception e) { e.printStackTrace(); } } }
package com.gd.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.gd.entity.Msg; public class MsgDao extends BaseDao{ public List<Msg> getAllMsg(String name){ List<Msg> MsgList=new ArrayList<Msg>(); Connection con=getConnection(); String sql="select * from email where shou=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); rs=ps.executeQuery(); while(rs.next()){ Msg em=new Msg(); em.setId1(rs.getInt(1)); em.setFa(rs.getString(2)); em.setShou(rs.getString(3)); em.setBiao(rs.getString(4)); em.setNei(rs.getString(5)); em.setTime(rs.getDate(6)); em.setZhuang(rs.getInt(7)); MsgList.add(em); } } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return MsgList; } }
package com.gd.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class UsersDao extends BaseDao{ //登录 public boolean DengLu(String name,String pwd) { boolean f=false; Connection con=getConnection(); String sql="select * from user where name=? and pwd=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); rs=ps.executeQuery(); if(rs.next()) f=true; } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return f; } //注册 public int ZhuCe(String name,String pwd){ int i=-1; Connection con=getConnection(); String sql="insert into user(name,pwd) values(?,?)"; PreparedStatement ps=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); i=ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, null); } return i; } }
package com.gd.entity; import java.util.Date; public class Msg { private int id1; //id private String shou; //收件人 private String biao; //标题 private String nei; //内容 private int zhuang; //状态 private String fa; //发件人 private Date time; //时间 public int getId1() { return id1; } public void setId1(int id1) { this.id1 = id1; } public String getShou() { return shou; } public void setShou(String shou) { this.shou = shou; } public String getBiao() { return biao; } public void setBiao(String biao) { this.biao = biao; } public String getNei() { return nei; } public void setNei(String nei) { this.nei = nei; } public int getZhuang() { return zhuang; } public void setZhuang(int zhuang) { this.zhuang = zhuang; } public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } }
package com.gd.entity; public class Users { private int id; private String name; private String pwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
<%@page import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>index.jsp</title> </head> <body> <% MsgDao mail=new MsgDao(); String user=(String) session.getAttribute("user"); List<Msg> MailList=mail.getAllMsg(user); %> 欢迎你:<%=user %> <br><br> <table border="1px" cellpadding="15px" align="center"> <tr> <td>编号</td> <td>发件人</td> <td>收件人</td> <td>标题</td> <td>内容</td> <td>时间</td> <td>状态</td> </tr> <%for(int i=0;i<MailList.size();i++){ %> <tr> <td><%out.print(MailList.get(i).getId1()); %></td> <td><%out.print(MailList.get(i).getFa()); %></td> <td><%out.print(MailList.get(i).getShou());%></td> <td><%out.print(MailList.get(i).getBiao()); %></td> <td><%out.print(MailList.get(i).getNei()); %></td> <td><%out.print(MailList.get(i).getTime()); %></td> <td><%out.print(MailList.get(i).getZhuang()); %></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>MyJsp.jsp</title> </head> <body> <form action="pan.jsp" method="post" name="form1"> 用户名:<input name="name" type="text"><br><br> 密码: <input name="password" type="password"><br><br> <input type="button" value="登录" onclick="d()"> <a href="MyJsp2.jsp">注册 </a> <script type="text/javascript"> function d() { if (form1.name.value == "") { alert("用户名不能为空!"); return; } if (form1.password.value == "") { alert("密码不能为空!"); return; } form1.submit(); } </script> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>MyJsp2.jsp</title> </head> <body> <form action="pan2.jsp" method="post" name="form2"> 用户名:<input name="name" type="text"><br><br> 密码: <input name="password1" type="password"><br><br> 确认密码:<input name="password2" type="password"><br><br> <input type="button" value="注册" onclick="z()"> <a href="MyJsp.jsp">返回</a> <script type="text/javascript"> function z() { if (form2.name.value == "") { alert("用户名不能为空!"); return; } if (form2.password1.value == "") { alert("密码不能为空!"); return; } if (form2.password2.value == "") { alert("确认密码不能为空!"); return; } if (form2.password1.value != form2.password2.value) { alert("密码不一致,请重新输入!"); return; } form2.submit(); } </script> </form> </body> </html>
<%@page import="com.gd.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>pan.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password = request.getParameter("password"); UsersDao ud = new UsersDao(); if (ud.DengLu(name, password)) { session.setAttribute("user", name); request.getRequestDispatcher("index.jsp").forward(request,response); } else { out.print("登陆失败!"); response.setHeader("refresh", "1;url=MyJsp.jsp"); } %> </body> </html>
<%@page import="com.gd.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>pan2.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password1 = request.getParameter("password1"); UsersDao ud = new UsersDao(); int i = ud.ZhuCe(name, password1); if (i > 0) { out.print("注册成功!"); response.setHeader("refresh", "1;url=MyJsp.jsp"); } else { out.print("注册失败!"); response.setHeader("refresh", "1;url=MyJsp2.jsp"); } %> </body> </html>