第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)


2.建model层
entity,dao包
package com.ymz;
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 conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
// 2.建立连接
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "admin");
} 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();
}
}
}
package com.ymz.Dao;
import java.sql.Date;
public class msg {
private Integer msgid;
private String username;
private String title;
private String msgcontent;
private Integer state;
private String sendto;
private Date msg_create_date;
public Integer getMsgid() {
return msgid;
}
public void setMsgid(Integer msgid) {
this.msgid = msgid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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 Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getSendto() {
return sendto;
}
public void setSendto(String sendto) {
this.sendto = sendto;
}
public Date getMsg_create_date() {
return msg_create_date;
}
public void setMsg_create_date(Date msg_create_date) {
this.msg_create_date = msg_create_date;
}
}
package com.ymz.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 javax.annotation.PreDestroy;
import com.ymz.Dao.msg;
import com.sun.org.apache.regexp.internal.recompile;
public class msgDao extends BaseDao{
public List<msg> listAll(String username) throws SQLException{
List<msg> list =new ArrayList<msg>();
Connection conn =getConnection();
String sql="select * from msg where username=?";
PreparedStatement pred =null;
ResultSet rest=null;
try {
pred=conn.prepareStatement(sql);
pred.setString(1, username);
rest=pred.executeQuery();
while (rest.next()) {
msg msg=new msg();
msg.setMsgid(rest.getInt("msgid"));
msg.setUsername(rest.getString("username"));
msg.setTitle(rest.getString("title"));
msg.setMsgcontent(rest.getString("msgcontent"));
msg.setState(rest.getInt("state"));
msg.setSendto(rest.getString("sendto"));
msg.setMsg_create_date(rest.getDate("msg_create_date"));
list.add(msg);
}
} catch (Exception e) {
// TODO: handle exception
}
finally{
closeAll(conn, pred, rest);
}
return list;
}
}
3.登陆,注册,登陆后显示全部邮件
<%@page import="java.util.List"%>
<%@page import="com.ymz.Dao.msgDao"%>
<%@page import="com.ymz.Dao.msg"%>
<%@page import="sun.security.jgss.LoginConfigImpl"%>
<%@page import="com.ymz.Dao.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>
<body>
<%
String uname=(String)session.getAttribute("uname");
if(uname==null){
out.print("您未登录,即将跳回登陆页.....");
response.setHeader("refresh", "5;url=login.jsp");
}else{
%>
欢迎你<%=uname %>
<%
msg msg=new msg();
msgDao msgDao =new msgDao();
List<msg> list =new ArrayList<msg>();
list=msgDao.listAll(uname);
%>
邮件列表:
<table width="600px" border="2px solid red">
<tr>
<th>id</th><th>发件人</th><th>标题</th><th>内容</th><th>已读未读状态</th><th>收件人</th><th>发送时间</th>
</tr>
<%
if(list.size()==0){
out.print("你没有邮件");
}else{
for(msg msg1:list){
%>
<tr>
<td><%out.print(msg1.getMsgid()); %></td>
<td><%out.print(msg1.getUsername()); %></td>
<td><%out.print(msg1.getTitle()); %></td>
<td><%out.print(msg1.getMsgcontent()); %></td>
<td><%out.print(msg1.getState()); %></td>
<td><%out.print(msg1.getSendto()); %></td>
<td><%out.print(msg1.getMsg_create_date()); %></td>
</tr>
<%}}} %>
</table>
</body>
</html>
<%@page import="com.ymz.Dao.usersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String uid=request.getParameter("id");
Integer id=Integer.parseInt(uid);
String uname=request.getParameter("uname");
String upwd =request.getParameter("upwd");
String upwd2 =request.getParameter("upwd2");
usersDao usersDao=new usersDao();
if(upwd.equals(upwd2)){
if(usersDao.id(id)){
out.print("已经有这个id了,即将跳回注册页.....");
response.setHeader("refresh", "5;url=zhuce.jsp");
}
else{
usersDao.zhuce(id, uname, upwd);
out.print("注册成功,即将跳回登录页.....");
response.setHeader("refresh", "5;url=login.jsp");
}
}
else{
out.print("两次密码不一致,即将跳回注册页.....");
response.setHeader("refresh", "5;url=zhuce.jsp");
}
%>
</body>
</html>
<%@ 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>
<body>
<script type="text/javascript">
function dologin(){
if(loginform.uname.value==""){
alert("没有输入用户名");
return;
}
if(loginform.upwd.value==""){
alert("没有输入密码");
return;
}
loginform.submit();
}
</script>
<form action="dologin.jsp" name="loginform" method="post">
用户名:<input type="text" value="zs" name="uname"/>
密码:<input type="password" value="11" name="upwd"/>
<input type="button" value="登录" onclick="dologin()"/>
<a href="zhuce.jsp">没有账号?立即注册</a>
</form>
</body>
</html>
<%@ 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>
<body>
<script type="text/javascript">
function dozhuce(){
if(loginform2.id.value==""){
alert("没有输入id");
return;
}
if(loginform2.uname.value==""){
alert("没有输入用户名");
return;
}
if(loginform2.upwd.value==""){
alert("没有输入密码");
return;
}
if(loginform2.upwd2.value==""){
alert("没有确认密码");
return;
}
loginform2.submit();
}
</script>
<form action="dozhuce.jsp" name="loginform2" method="post">
id: <input type="number" name="id"/>
用户名:<input type="text" value="zs" name="uname"/>
密码:<input type="password" value="11" name="upwd"/>
确认密码:<input type="password" value="11" name="upwd2"/>
<input type="button" value="注册" onclick="dozhuce()"/>
</form>
</body>
</html>

浙公网安备 33010602011771号