十一

package com.dmk.dao;

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

import com.lwk.email.entity.User;
import com.lwk.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.dmk.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.lwk.email.entity.Msg;
import com.lwk.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.dmk.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.dmk.email.entity.User"%>
<%@page import="com.dmk.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.dmk.email.dao.UserDao"%>
<%@page import="com.dmk.email.entity.User"%>
<%@ 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");
    User user = new User(uname,password);
    UserDao userDao = new UserDao();
    userDao.userReg(user);
    response.sendRedirect("login.jsp");
 %>
</body>
</html>

 

posted @ 2022-05-15 20:07  董梦坤  阅读(34)  评论(0编辑  收藏  举报