十一周作业

1.建库,建表2个

用户表(id,用户名,密码)

邮件表(id,发件人,收件人,标题,内容,发送时间,状态)

2.建model层

entity,dao包

3.登陆,注册,登陆后显示全部邮件

5个页面   login,dologin,reg,doreg,main

 

 

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();
        }
         
    }
}
package com.yzy.dao;

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 com.yzy.entity.Msg;

public class MsgDao extends BaseDao{
    public List<Msg> getAll(String uname){
        List<Msg>list=new ArrayList<Msg>();
        Connection con=getConnection();
        PreparedStatement pred=null;
        ResultSet resultSet=null;
        String sql="select * from email where sjr=?";
        try {
            pred=con.prepareStatement(sql);
            pred.setString(1, uname);
            resultSet=pred.executeQuery();
            while(resultSet.next()){
                Msg msg=new Msg();
                msg.setId(resultSet.getInt(1));
                msg.setFjr(resultSet.getString(2));
                msg.setSjr(resultSet.getString(3));
                msg.setTitle(resultSet.getString(4));
                msg.setContent(resultSet.getString(5));
                msg.setTime(resultSet.getDate(6));
                msg.setZt(resultSet.getInt(7));
                list.add(msg);
            }
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        return list;
    }

}
package com.cui.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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;
        
        
    }
}
package com.cui.entity;

import java.sql.Date;

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;
    }
    
    

}
package com.cui.entity;

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.yzy.dao.UsersDao"%>
<%@ 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 uname = request.getParameter("uname");
        String password = request.getParameter("password");
        UsersDao ud = new UsersDao();
        if(ud.Login(uname, password)){
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("main.jsp").forward(request, response);
        }else{
        out.print("登录失败,即将跳回登录页.....");
        response.setHeader("refresh", "2;url=denglu.jsp");
        }
    %>

</body>
</html>
<%@page import="com.yzy.entity.Msg"%>
<%@page import="com.yzy.dao.MsgDao"%>
<%@page import="com.yzy.dao.UsersDao"%>
<%@ 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 uname = (String) session.getAttribute("uname");
        MsgDao dao = new MsgDao();
        List<Msg> list = dao.getAll(uname);
    %>
    欢迎<%=uname%>
    <br>
    <br>
    <table border="1px" cellspacing="0" cellpadding="30px">
        <thead></thead>
        <tr>
            <th>序号</th>
            <th>发件人</th>
            <th>收件人</th>
            <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).getId().toString()); %></td>
            <td><%out.print(list.get(i).getFjr().toString()); %></td>
            <td><%out.print(list.get(i).getSjr().toString()); %></td>
            <td><%out.print(list.get(i).getTitle().toString()); %></td>
            <td><%out.print(list.get(i).getContent().toString()); %></td>
            <td><%out.print(list.get(i).getTime().toString()); %></td>
            <td><%if(list.get(i).getZt()==0){
            out.print("未读");
            }else{
            out.print("已读");
            }%></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> </head>
  
  <body>
 <form action="doregister.jsp" method="post">
 账号:<input type="text" name="uname"><br>
 密码:<input type="text" name="upwd"><br>
<input type="submit"value="注册">
<input type="reset" value="重置 ">
 </form>
  </body>
</html>

 

 

 

posted @ 2022-05-15 10:10  kgbk  阅读(7)  评论(0编辑  收藏  举报