第十一周作业

package com.gd.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 Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "123456");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


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

public class MsgDao extends BaseDao {
    // 1,插入邮件

    // 2.删除邮件

    // 3.修改邮件状态

    // 4.按照接收者查询全部邮件
    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 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) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }
        return list;

    }

}
package com.gd.dao;

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

public class UsersDao extends BaseDao {
    // 关于用户的增删改查

    // 1.登录
    public boolean login(String uname, String upwd) {
        boolean f = false;
        Connection con = getConnection();
        String sql = "select * from users where uname=? and upwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return f;
    }

    // 2.注册
    public int register(String uname, String upwd) {
        Connection con = getConnection();
        PreparedStatement ps = null;
        int x = 0;
        try {
            String sql = "insert into users(uname,upwd) values(?,?)";
            ps = con.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            x = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, null);
        }
        return x;
    }
}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body >
    <script type="text/javascript">
        function validate() {
            if (loginForm.uname.value == "") {
                alert("账号不能为空!");
                return;
            }
            if (loginForm.upwd.value == "") {
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>
    <form name="loginForm" action="dologin.jsp" method="post">
    用户名:<input type="text" name="uname" ><br>
    密 码:<input type="password" name="upwd" ><br>
           <input type="button" value="登录" onClick="validate()">
    </form>
    <hr>
    <a href="reg.jsp">返回注册</a>
</body>
</html>
<%@page import="com.gd.dao.UsersDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    
    <%
        request.setCharacterEncoding("utf-8");
        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=denglu.jsp");
        }
    %>
</body>
</html>

 

<%@page import="com.gd.dao.MsgDao"%>
<%@page import="com.gd.dao.UsersDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");

        UsersDao ud = new UsersDao();
        MsgDao md = new MsgDao();
        if (ud.register(uname, upwd) > 0) {
            session.setAttribute("uname", uname);
            request.getRequestDispatcher("denglu.jsp").forward(request,
                    response);
        } else {
            out.print("注册失败,请重新注册.......");
            response.setHeader("refresh", "3;url=reg.jsp");
        }
    %>

</body>
</html>
<%@page import="com.gd.entity.Msg"%>
<%@page import="java.util.List"%>
<%@page import="com.gd.dao.MsgDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
        String uname = (String) session.getAttribute("uname");
    %>
    欢迎你<%
        out.print(uname);
    %>使用邮箱
    <hr>
    <table >
        <tr>
            <td>id</td>
            <td>发件人</td>
            <td>标题</td>
            <td>内容</td>
            <td>已读未读状态</td>
            <td>收件人</td>
            <td>发送时间</td>
        </tr>
        <%
            MsgDao md = new MsgDao();
            List<Msg> list = md.getMailByReceiver(uname);
            for (Msg m : list) {
        %>
        <tr>
            <td><%=m.getMsgid()%></td>
            <td><%=m.getUsername()%></td>
            <td><%=m.getTitle()%></td>
            <td><%=m.getMsgcontent()%></td>
            <td>
                <%
                    int state = m.getState();
                        if (state == 1) {
                %> <img src="image/yidu.png"> <%
     ;
         } else {
 %> <img src="image/weidu.png"> <%
     ;
         }
 %>
            </td>
            <td><%=m.getSendto()%></td>
            <td><%=m.getMsg_create_date()%></td>
        </tr>
        <%
            }
        %>
    </table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
        function validate() {
            if (registerForm.uname.value == "") {
                alert("账号不能为空!");
                return;
            }
            if (registerForm.upwd.value == "") {
                alert("密码不能为空!");
                return;
            }
            registerForm.submit();
        }
    </script>

    <form name="registerForm" action="doreg.jsp" method="post">
        用户名:<input type="text" name="uname"><br> 
        密   码: <input type="password" name="upwd"><br> 
        <input type="submit" value="注册"><hr>
    </form><hr>
    <a href="denglu.jsp">返回登录</a>

</body>
</html>

 

 

 

 

 

posted @ 2022-05-15 21:46  王智达  阅读(5)  评论(0编辑  收藏  举报