第十一周作业

1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)

2.建model层
entity,dao包

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

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

public class MsgDao extends BaseDao{
    //关于邮件的增删改查    
    //添加邮件(写邮件,回复邮件都调用,邮件状态为0,时间为系统当前时间)
    
    
    //根据id删除邮件
    
    
    //根据id修改邮件状态(统一改成1)
    
    
    //根据收件人查看全部邮件
    public List<Msg> getMailByReceiver(String name){
        List<Msg> list=new ArrayList<Msg>();
        Connection con=getConnection();
        String sql="select * from msg where sendto=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, name);
             rs=ps.executeQuery();
            while(rs.next()){
                //每读取一行,创建一个msg对象,对象放到集合中
                Msg m=new Msg();
                m.setMsgid(rs.getInt(1));
                m.setUsername(rs.getString(2));
                m.setTitle(rs.getString(3));
                m.setMsgcontent(rs.getString(4));
                m.setState(rs.getInt(5));
                m.setSendto(rs.getString(6));
                m.setMsg_create_date(rs.getDate(7));
                list.add(m);                
            }            
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }        
        return list;
    }
    
    
    
    

}
package com.jw.entity;

import java.util.Date;

public class Msg {
    private int msgid;
    private String username;
    private String title;
    private String msgcontent;
    private int state;
    private String sendto;
    Date msg_create_date;
    public int getMsgid() {
        return msgid;
    }
    public void setMsgid(int msgid) {
        this.msgid = msgid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getMsgcontent() {
        return msgcontent;
    }
    public void setMsgcontent(String msgcontent) {
        this.msgcontent = msgcontent;
    }
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    public String getSendto() {
        return sendto;
    }
    public void setSendto(String sendto) {
        this.sendto = sendto;
    }
    public Date getMsg_create_date() {
        return msg_create_date;
    }
    public void setMsg_create_date(Date msg_create_date) {
        this.msg_create_date = msg_create_date;
    }
}
package com.jw.dao;

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

public class UsersDao extends BaseDao{
    public int Register(String uname,String password){
        int i=-1;
        PreparedStatement pred=null;
        Connection con=getConnection();
        String sql="insert into users(uname,password)values(?,?)";
        try {
            pred= con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            i=pred.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, pred, null);
        }
        return i;
    }
    
    public boolean Login(String uname,String password){
        boolean f=false;
        PreparedStatement pred=null;
        ResultSet resultSet=null;
        Connection con=getConnection();
        String sql="select * from users where uname=? and password=?";
        try {
            pred=con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            resultSet=pred.executeQuery();
            while(resultSet.next()){
                f=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return f;
    }
    
}
package com.jw.dao;

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","root");
        } 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 (SQLException e) {
                e.printStackTrace();
            }
        }
}
<%@page import="com.jw.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>
<h1>登录</h1>
    <script>
        function yz() {
            if (form.uname.value == null) {
                alert('用户名不能为空');
                return;
            }
            if (form.password.value == null) {
                alert('密码不能为空');
                return;
            }
            if (form.uname.value != null && form.password.value != null) {
                form.submit();
            }
        }
    </script>
    <form action="dologin.jsp" method="post" name="form">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="uname"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password"></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="com.jw.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.jw.entity.Msg"%>
<%@page import="com.jw.dao.MsgDao"%>
<%@page import="com.jw.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"%>
<%
    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.uname.value == "") {
                alert('用户名不能为空');
                return;
            }
            if (form.password.value == "") {
                alert('密码不能为空');
                return;
            }
            form.submit();
        }
    </script>
    <form action="denglu.jsp" method="post" name="form">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="uname">
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="upwd">
                </td>
            </tr>
            <tr>
                <td><input type="button" value="注册" onclick="yz()">
                </td>
                <td><a href="denglu.jsp">登录</a>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

 

 

 

posted @ 2022-05-15 14:54  ♡十梦九你♡  阅读(15)  评论(0编辑  收藏  举报