11

package com.zjh.email.dao;

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

import com.zjh.email.entity.User;
import com.zjh.email.util.DbUtil;

public class UserDao {
    public void userReg(User user){
        String sql = "insert into user(uname,password) value ('"+ user.getUname() +"','"+user.getPassword()+"')";
        Connection con = DbUtil.getCon();
        try {
            PreparedStatement pred = con.prepareStatement(sql);
//            pred.setString(1, user.getUname());
//            pred.setString(2, user.getPassword());
//            pred.setInt(3, user.getAge());
            pred.executeUpdate();
            pred.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public User getUserdentByName(String uname){
        String sql = "select * from user where uname='"+uname+"'";
        Connection con = null;
        PreparedStatement pred = null;
        ResultSet res = null;
        con = DbUtil.getCon();
        try {
            pred = con.prepareStatement(sql);
//            pred.setString(1, uname);
            res = pred.executeQuery();
            User user = null;
            if (res.next()) {
                user = new User(res.getString("uname"), res.getString("password"));
            }
            return user;
        } catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace(); }finally{ DbUtil.closeAll(con, pred, res); } return null; } }
package com.zjh.email.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.zjh.email.entity.Msg;
import com.zjh.email.util.DbUtil;

public class MsgDao {

    public List<Msg> getMailByReceiver(String name) {
        List<Msg> list = new ArrayList<Msg>();
        Connection con = DbUtil.getCon();
        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("eid"));
                m.setUsername(rs.getString("uname"));
                m.setTitle(rs.getString("title"));
                m.setMsgcontent(rs.getString("msgcontent"));
                m.setState(rs.getInt("state"));
                m.setSendto(rs.getString("sendto"));
                m.setMsg_create_date(rs.getDate("msg_create_date"));
                list.add(m);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
        }
        return list;

    }

}
package com.zjh.email.util;

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

public class DbUtil {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Connection getCon() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/email", "root", "123456");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }

    // 关闭连接
    public static 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();
        }
    }
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title>登录</title>
</head>
<body>
    <form name="formName" action="dologin.jsp" method="post">
        用户名:<input type="text" name="uname"><br> 密码:<input
            type="password" name="password"><br> <input
            type="button" value="登录" onclick="toDoLogin()"> <input
            type="button" value="注册" onclick="toReg()">
    </form>
    <script>
        function toReg() {
            window.location.href = "reg.jsp";
        }
        function toDoLogin() {
            if (formName.uname.value == "" || formName.password.value == "") {
                alert("用户名或密码不能为空");
            }else{
                formName.submit();
            }
        }
    </script>
</body>
</html>
<%@page import="com.zjh.email.entity.User"%>
<%@page import="com.zjh.email.dao.UserDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
    <%
        request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            String uname = request.getParameter("uname");
            String password = request.getParameter("password");
            UserDao userdentDao = new UserDao();
            User user = userdentDao.getUserdentByName(uname);
            if (password.equals(user.getPassword())) {
            session.setAttribute("uname", uname);
        response.sendRedirect("index.jsp");
            } else {
        response.sendRedirect("login.jsp");
            }
    %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title>注册</title>
</head>
<body>
    <form action="doreg.jsp" method="post">
        用户名:<input type="text" name="uname"><br>
        密码:<input type="password" name="password"><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
<%@page import="com.zjh.email.dao.UserDao"%>
<%@page import="com.zjh.email.entity.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    //接收表单传值
    String uname = request.getParameter("uname");
    String password = request.getParameter("password");
    User user = new User(uname,password);
    UserDao userDao = new UserDao();
    userDao.userReg(user);
    response.sendRedirect("login.jsp");
 %>
</body>
</html>
<%@page import="com.zjh.email.entity.Msg"%>
<%@page import="com.zjh.email.dao.MsgDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<style>
table {
    border: 2px solid black;
    border-collapse: collapse;
}
table tr td{
border: 2px solid black
}
</style>
</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>

 

posted @ 2022-05-15 19:48  赵佳慧  阅读(51)  评论(0编辑  收藏  举报