• Jsp第七次作业email


     BaseDao.java

    package com.gd.dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
    public class BaseDao {
    
        //获取连接
        protected Connection getConnection(){
            Connection conn=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    // 2.建立连接
                    conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/user", "root", "root");
                } catch (Exception e) {
                    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) {
                e.printStackTrace();
            }
        }
        
    }

    MsgDao.java

    package com.gd.dao;
    
    public class MsgDao {
        //发送,回复---insert操作
        
        //邮件列表  --select * from  msg where username=....
        
        
        //根据标题查内容   select 
        
        //阅读状态改变,,,未读 已读   update
        
        //删除邮件  delete 
    
    }

    UsersDao.java

    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 {
        // 登录功能
        public boolean login(String uname, String upwd) throws SQLException {
            // 获取连接
            Connection conn = getConnection();
            // 编写sql语句
            String sql = "select * from users where username=? and password=?";
            // 执行sql语句
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                closeAll(conn, ps, rs);
                return true;
            } else {
                closeAll(conn, ps, rs);
                return false;
            }
        }
    //    public static void main(String[] args) {
    //        UsersDao ud=new UsersDao();
    //        try {
    //            System.out.println(ud.login("tom", "456"));
    //        } catch (SQLException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //    }
    
    }

    Msg.java

    package com.gd.entity;
    
    public class Msg {
        int msgid;
        String usernname;
        String title;
        String msgcontent;
        int state;
        String sendto;
        String msg_create_date;
        public int getMsgid() {
            return msgid;
        }
        public void setMsgid(int msgid) {
            this.msgid = msgid;
        }
        public String getUsernname() {
            return usernname;
        }
        public void setUsernname(String usernname) {
            this.usernname = usernname;
        }
        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 String getMsg_create_date() {
            return msg_create_date;
        }
        public void setMsg_create_date(String msg_create_date) {
            this.msg_create_date = msg_create_date;
        }
    
    }

    Users.java

    
    

    package com.gd.entity;

    
    

    public class Users {
    String username;
    String password;
    String email;
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public String getEmail() {
    return email;
    }
    public void setEmail(String email) {
    this.email = email;
    }

    
    

    }

     

    newfile.sql

    drop table users;
    drop table msg;
    
    CREATE TABLE users(
    username VARCHAR(20) PRIMARY KEY,
    PASSWORD VARCHAR(20),
    email VARCHAR(20)
    )
    
    
    CREATE TABLE `msg` (
      `msgid` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(100) NOT NULL,
      `title` varchar(20) NOT NULL,
      `msgcontent` varchar(20) NOT NULL,
      `state` int(11) NOT NULL,
      `sendto` varchar(20) NOT NULL,
      `msg_create_date` datetime NOT NULL,
      PRIMARY KEY (`msgid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    insert into users values('ls','456','aaaa@qq.com');
    insert into users values('kitty','777','bbb@qq.com');
    insert into users values('tom','456','abc@qq.com');
    insert into users values('小红','456','88888@qq.com');
    insert into users values('小白','88888','22222@qq.com');
    
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('kitty','你好','兴隆又打折了,快来吧',0,'小白','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('kitty','打折了','食堂打折了,快来吧',1,'小红','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('kitty','系统非功能性需求分析','系统非功能性需求分析',0,'tom','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('kitty','收作业','兴隆又打折了,快来吧',0,'小白','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('ls','要考试了','兴隆又打折了,快来吧',1,'kitty','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('ls','放假通知','兴隆又打折了,快来吧',0,'小白','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('kitty','邀请函','系统功能模块设计',1,'ls','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('ls','毕业答辩须知','兴隆又打折了,快来吧',1,'小白','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('kitty','考研讲座','兴隆又打折了,快来吧',1,'小红','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('zs','四六级考试通知','兴隆又打折了,快来吧',0,'小白','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('kitty','交学费','兴隆又打折了,快来吧',1,'小红','2019-04-24');
    
    insert into msg(username,title,msgcontent,state,sendto,msg_create_date)
    values('kitty','毕业实习通知','兴隆又打折了,快来吧',0,'小红','2019-04-24');

    dologin.jsp

    <%@page import="com.gd.dao.UsersDao"%>
    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud = new UsersDao();
        if (ud.login(uname, upwd))
            request.getRequestDispatcher("main.jsp").forward(request, response);
        else
            response.sendRedirect("index.jsp");
    %>
    </html>

    index.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
        
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
       <form action="dologin.jsp" method="post">
    用户名:<input type="text" name="uname" value="kitty" /><Br>
    密码 :<input type="password" name="upwd" value="777"/><br>
    
    <input type="submit" value="登录">
    
    </form>
      </body>
    </html>

    main.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
        <%@page import="java.sql.SQLException"%>
    <%@page import="com.gd.dao.BaseDao"%>
    <%@page import="com.gd.entity.Msg"%>
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.ResultSet"%>
    
    <%@page import="java.sql.Connection"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <%
            String uname = request.getParameter("uname");
            session.setAttribute("uname", uname);
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                //加载驱动 
    
                String url = "jdbc:mysql://localhost:3306/user";
                String user = "root";
                String password = "root";
                conn = DriverManager.getConnection(url, user, password);
                ps = conn.prepareStatement("select * from msg where username=?");
                ps.setString(1, uname);
                rs = ps.executeQuery();
        %>
        <table>
            <tr>
            <td>msgid</td>      
            <td>username</td>
                <td>title</td>
                <td>msgcontent</td>
                <td>state</td>
                <td>sendto</td>
                <td>msg_create_date</td>
            </tr>
            <%
                while (rs.next()) {
            %>
            <tr>
                <td><%=rs.getString("username")%></td>
                <td><%=rs.getString("title")%></td>
                <td><%=rs.getString("msgcontent")%></td>
    
                <td>
                    <%
                        if (rs.getString("state").equals("1")) {
                    %> <input type="button" value="点击查看"
                    onclick="window.location.href='show.jsp';" /> <%
         } else {
     %>
                    <div align="center">
                        <%
                            out.print("已查看");
                        %>
                    </div> <%
         }
     %> <%
    
         String title=rs.getString("title");
         session.setAttribute("title", title);
         
     %>
                </td>
                <td><%=rs.getString("sendto")%></td>
                <td><%=rs.getString("msg_create_date")%></td>
            </tr>
            <%
                }
            %>
        </table>
        <br>
    
        <%
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) { // ignore }
                        rs = null;
                    }
                }
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) { // ignore }
                        ps = null;
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) { // ignore }
                        rs = null;
                    }
                }
            }
        %>
    
    </body>
    </html>

     

     

     数据库图片:

    users:

    msg:

  • 相关阅读:
    antd pro2.0 使用记录五:设置代理
    antd pro2.0 使用记录四:右侧顶部菜单栏+新建页面
    antd pro2.0 使用记录三:多个接口调用问题
    antd pro2.0 记录二:登录/注册页面逻辑,调用后台
    实用的Portraiture滤镜磨皮教程
    mysql 远程访问权限
    vc6.0运用mysql数据库中的编码所导致的乱码问题(接收和输出的编码必须要一致)
    飞思卡尔imx6开发板Linux下GPIO驱动
    beagleBone black 中QT的移植
    Beaglebone Black从零开始系列教程大汇总!
  • 原文地址:https://www.cnblogs.com/gjm717/p/12823976.html
Copyright © 2020-2023  润新知