第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
package
myh;
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","990511"); } catch (Exception e) { System.out.println("连接失败"); 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
myh;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import glg2.Email; public class EmailDao extends BaseDao{ public List<Email> getAll(String name){ List<Email>list=new ArrayList<Email>(); Connection con=getConnection(); PreparedStatement pred=null; ResultSet resultSet=null; String sql="select * from email where addressee=?"; try { pred=con.prepareStatement(sql); pred.setString(1, name); resultSet=pred.executeQuery(); while(resultSet.next()){ Email email=new Email(); email.setId(resultSet.getInt(1)); email.setAddresser(resultSet.getString(2)); email.setAddressee(resultSet.getString(3)); email.setHead(resultSet.getString(4)); email.setContent(resultSet.getString(5)); email.setTime(resultSet.getDate(6)); email.setState(resultSet.getInt(7)); list.add(email); } } catch (SQLException e) { e.printStackTrace(); } return list; } }
package
myh;
import java.util.Date; public class Email { private Integer id; private String addresser; private String addressee; private String head; private Date time; private Integer state; private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAddresser() { return addresser; } public void setAddresser(String addresser) { this.addresser = addresser; } public String getAddressee() { return addressee; } public void setAddressee(String addressee) { this.addressee = addressee; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Email() { super(); } public Email(Integer id, String addresser, String addressee, String head, Date time, Integer state, String content) { super(); this.id = id; this.addresser = addresser; this.addressee = addressee; this.head = head; this.time = time; this.state = state; this.content = content; } }
package
myh2;
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 name,String pwd){ int i=-1; PreparedStatement pred=null; Connection con=getConnection(); String sql="insert into name(name,pwd)values(?,?)"; try { pred= con.prepareStatement(sql); pred.setString(1, name); pred.setString(2, pwd); i=pred.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(con, pred, null); } return i; } public boolean Login(String name,String pwd){ boolean f=false; PreparedStatement pred=null; ResultSet resultSet=null; Connection con=getConnection(); String sql="select * from name where name=? and pwd=?"; try { pred=con.prepareStatement(sql); pred.setString(1, name); pred.setString(2, pwd); resultSet=pred.executeQuery(); while(resultSet.next()){ f=true; } } catch (SQLException e) { e.printStackTrace(); } return f; } }
package
myh2;
public class Users { private Integer id; private String name; private String pwd; public Integer getId() { return id; } public void setId(Integer 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; } public Users() { super(); } public Users(Integer id, String name, String pwd) { super(); this.id = id; this.name = name; this.pwd = pwd; } }
<%@page import="glg.UserDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML> <html> <head> <base href="<%=basePath%>"> <title></title> </head> <body> <h1>登录</h1> <script> function yz() { if (form.name.value == null) { alert('用户名不能为空'); return; } if (form.pwd.value == null) { alert('密码不能为空'); return; } if (form.name.value != null && form.pwd.value != null) { form.submit(); } } </script> <form action="dologin.jsp" method="post" name="form"> <table> <tr> <td>用户名</td> <td><input type="text" name="name"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="pwd"></td> </tr> <tr> <td><input type="button" value="登录" onclick="yz()"></td> <td><a href="register.jsp">注册</a></td> </tr> </table> </form> </body> </html>
<%@page import="glg.UserDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML> <html> <head> <base href="<%=basePath%>"> <title></title> </head> <body> <% String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); UserDao ud = new UserDao(); if(ud.Login(name, pwd)){ session.setAttribute("name", name); request.getRequestDispatcher("main.jsp").forward(request, response); }else{ out.print("登录失败,即将跳回登录页....."); response.setHeader("refresh", "2;url=Login.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML> <html> <head> <base href="<%=basePath%>"> <title>注册</title> </head> <body> <h1>注册</h1> <script> function yz() { if (form.name.value == "") { alert('用户名不能为空'); return; } if (form.pwd.value == "") { alert('密码不能为空'); return; } form.submit(); } </script> <form action="doregister.jsp" method="post" name="form"> <table> <tr> <td>用户名</td> <td><input type="text" name="name"> </td> </tr> <tr> <td>密码</td> <td><input type="password" name="pwd"> </td> </tr> <tr> <td><input type="button" value="注册" onclick="yz()"> </td> <td><a href="Login.jsp">登录</a> </td> </tr> </table> </form> </body> </html>
<%@page import="glg.UserDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML> <html> <head> <base href="<%=basePath%>"> <title></title> </head> <body> <% String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); UserDao ud = new UserDao(); int i=ud.Register(name, pwd); if(i>0){ out.print("注册成功,即将跳到登录页....."); response.setHeader("refresh", "2;url=Login.jsp"); }else{ out.print("注册失败,即将跳回注册页....."); response.setHeader("refresh", "2;url=register.jsp"); } %> </body> </html>
<%@page import="glg2.Email"%> <%@page import="glg.EmailDao"%> <%@page import="glg.UserDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML> <html> <head> <base href="<%=basePath%>"> <title></title> </head> <body> <% String name = (String) session.getAttribute("name"); EmailDao dao = new EmailDao(); List<Email> list = dao.getAll(name); %> 欢迎<%=name%> <br> <br> <table border="1px" cellspacing="0" cellpadding="30px"> <thead></thead> <tr> <th>发件人</th> <th>标题</th> <th>时间</th> <th>状态</th> </tr> <tbody></tbody> <% for (int i = 0; i < list.size(); i++) { %> <tr> <td><%out.print(list.get(i).getAddresser().toString()); %></td> <td><%out.print(list.get(i).getHead().toString()); %></td> <td><%out.print(list.get(i).getTime().toString()); %></td> <td><%if(list.get(i).getState()==0){ out.print("<img src='img/w.png'></img>"); }else{ out.print("<img src='img/y.png'></img>"); }%></td> </tr> <% } %> </table> </body> </html>