package com.cui.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class basedao { protected static Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/email?useUnicode=true&characterEncoding=utf8","root","123456"); System.out.println(conn); } catch (Exception e) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class messagedao extends basedao{ public List<message> getAll(String uname){ List<message>list=new ArrayList<message>(); Connection con=getConnection(); PreparedStatement pred=null; ResultSet resultSet=null; String sql="select * from email where receiver=?"; try { pred=con.prepareStatement(sql); pred.setString(1, uname); resultSet=pred.executeQuery(); while(resultSet.next()){ message email=new message(); email.setMid(resultSet.getInt(1)); email.setUsername(resultSet.getString(2)); email.setReceiver(resultSet.getString(3)); email.setTitle(resultSet.getString(4)); email.setText(resultSet.getString(5)); email.setSendtime(resultSet.getDate(6)); email.setState(resultSet.getInt(7)); list.add(email); } } catch (SQLException e) { e.printStackTrace(); } return list; } }
public class usersdao extends basedao{ public boolean login(String uname,String upwd){ boolean f=false; Connection conn=getConnection(); String sql="select * from users where uname=? and upwd=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps= conn.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); rs=ps.executeQuery(); while(rs.next()){ f=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeall(conn, ps, rs); } return f; } public int register(String uname,String upwd){ int i=-1; Connection con=basedao.getConnection(); String sql="insert into users(uname,upwd)values(?,?)"; PreparedStatement pred = null; try { pred=con.prepareStatement(sql); pred.setString(1, uname); pred.setString(2, upwd); i =pred.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeall(con, pred, null); } return i; } }
public class message { private int mid; //编号 private String username; //发件人姓名 private String receiver; //收件人姓名 private String title; //标题 private String text; //内容 private Date sendtime; //发送时间 private int state; //状态 public int getMid() { return mid; } public void setMid(int mid) { this.mid = mid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Date getSendtime() { return sendtime; } public void setSendtime(Date sendtime) { this.sendtime = sendtime; } public int getState() { return state; } public void setState(int state) { this.state = state; } }
public class users { private int uid; //编号 private String uname; //账号 private String upwd; //密码 public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } }
<%@page import="com.cui.dao.messagedao"%> <%@page import="com.cui.dao.usersdao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); usersdao ud=new usersdao(); if(ud.login(uname, upwd)){ session.setAttribute("uname", uname); request.getRequestDispatcher("main.jsp").forward(request,response); }else{ out.print("登陆失败,三秒后返回登录界面"); response.setHeader("refresh", "3;url=login.jsp"); } %> </body> </html>
<%@page import="com.cui.dao.usersdao"%> <%@page import="javax.xml.bind.ParseConversionEvent"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); usersdao u=new usersdao(); int i=u.register(uname, upwd); if (i > 0) { out.print("注册成功,三秒后返回登录界面"); response.setHeader("refresh", "3;url=login.jsp"); } else { out.print("注册失败,三秒后返回注册界面"); response.setHeader("refresh", "3;url=register.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <form action="dologin.jsp" method="post"> 账号:<input type="text" name="uname" ><br> 密码:<input type="upwd" name="upwd" ><br> <input type="submit" value=登录> <a href="register.jsp" >注册</a> </form> </body> </html> <%@page import="com.cui.entity.message"%> <%@page import="com.cui.dao.messagedao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% messagedao md=new messagedao(); String name=(String)session.getAttribute("uname"); List<message> list=md.getAll(name);%> 欢迎你<%=name %> <table> <tr><td>编号</td> <td>发件人</td> <td>收件人</td> <td>标题</td> <td>内容</td> <td>发送时间</td> <td>状态</td> </tr> <%for(int i=0;i<list.size();i++){%> <tr><td><%out.print(list.get(i).getMid());%></td> <td><%out.print(list.get(i).getUsername());%></td> <td><% out.print(list.get(i).getReceiver());%></td> <td><%out.print(list.get(i).getTitle());%></td> <td><% out.print(list.get(i).getText());%></td> <td><% out.print(list.get(i).getSendtime());%></td> <td><% out.print(list.get(i).getState());%></td></tr> <%}%> </table> </body> </html>