BaseDao.java


package cn.itcast.chap10.Test;

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( "dbc:mysql://localhost:3306/comser", "root", "root");
                return conn;
            } 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();
        }
    }
 

Msg.java

package cn.itcast.chap10.Test;

public class Msg {
    //对msg数据库进行封装
    private int msgid;
    private String usernname;
    private String title;
    private String msgcontent;
    private int state;
    private String sendto;
    private 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;
    }
    
    

}

MsgDao.java

 

package cn.itcast.chap10.Test;

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

}

 Users.java

package cn.itcast.chap10.Test;

public class Users {
    //封装comser数据库数据
    private  String username;
    private  String password;
    private  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;
    }
    
    
}

UsersDao.java

package cn.itcast.chap10.Test;

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 comser 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;
        }
    }
}

数据库代码

CREATE TABLE comser(
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('litao','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');

Index.jsp

<%@ page language="java" import="java.util.*" 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">
<html>
  <head>
   
  </head>
  
  <body>
  <form action="dologin.jsp" method="post">
        用户名:<input type="text" name="uname" value="litao" /><Br>
密码 :<input type="password" name="upwd" value="777"/><br>
<input type="submit" value="登录"> </form> </body> </html>

 dologin.jsp

<%@page import="cn.itcast.chap10.Test.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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>

 

main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

 <%@page import="java.sql.SQLException"%>
<%@page import="cn.itcast.chap10.Test.BaseDao"%>
<%@page import="cn.itcast.chap10.Test.Msg"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.Connection"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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/comser";
            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){
                rs =null;
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) { // ignore }
                    ps = null;
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) { // ignore }
                    conn = null;
                }
            }
        }
    %>

  </body>
</html>

 图片