javaWeb作业之查看邮件详细信息(利⽤jQuery的ajax⽅法实
现)
1、使⽤jQuery的Ajax功能,完成查询邮箱功能。
每隔五秒刷新⼀次即查询数据库将信息加载到当前页⾯,
每次查询到邮件的信息包括:发件⼈,发送时间,邮件内容。
创建数据库:
DAO层代码:
package pojo;
import java.sql.Timestamp;
import java.util.Date;
//邮件信息实体类
public class Mail {
private int id;
private String name;
private Timestamp date;
private String content;
public Mail() {
}
public Mail(int id, String name, Timestamp date, String content) {  this.id = id;
this.name = name;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
}
}
package util;
import java.sql.*;
import java.util.*;
import pojo.Mail;
hange.v2.c3p0.ComboPooledDataSource;
/
/通过数据库连接池获取数据库连接
public class DBUtil {
private static int pageSize=3; //定义每页查询最多信息条数
private static ComboPooledDataSource ds; //定义全局变量数据库连接池对象
static{
ds = new ComboPooledDataSource(); //静态代码块中进⾏数据库连接池对象实例化  }
public static List<Mail> findMail(int page){
List<Mail> list = new ArrayList<Mail>(); //实例化⼀个ArrayList集合来存储邮件信息    Connection conn = null;
PreparedStatement ps = null;  //定义数据库预处理语句对象
ResultSet rs = null;
try {
conn = ds.getConnection(); //通过数据库连接池获取数据库连接对象
String sql = "select id,name,time,context from email limit ?,?"; //定义数据库查询语句  ps = conn.prepareStatement(sql);
ps.setInt(1, (page - 1) * pageSize);
ps.setInt(2, pageSize);
rs = ps.executeQuery(); //将执⾏后的结果放到rs结果集对象实例中
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
Timestamp date = rs.getTimestamp("time");
String content = rs.getString("context");
Mail mail = new Mail(id, name, date, content);
list.add(mail);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
// 根据pageSize与总条数计算总页数
public static int totalPage(){
int count=0;
Connection conn = null;jquery在线库
PreparedStatement ps = null;
ResultSet rs = null;
try {
Connection();
String sql="select count(*) from student";
ps=conn.prepareStatement(sql);
uteQuery();
()){
Int(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
rs.close();
ps.close();
conn.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return count%pageSize==0?count/pageSize:count/pageSize+1;
}
//获取每个邮件详细信息
public static Mail getMailInfo(int id){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Mail mail = null; //定义邮件对象为全局变量,以便于作为返回值
try {
conn = ds.getConnection();
String sql = "select id,name,time,context from email where id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs =ps.executeQuery();
()){
int mailid = rs.getInt("id");
String name = rs.getString("name");
Timestamp date = rs.getTimestamp("time");
String context = rs.getString("context");
mail = new Mail(mailid,name,date,context);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{ //关闭资源
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return mail;
}
}
前端页⾯代码:
<%@page import="java.URLDecoder"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>邮件信息概览</title>
</head>
<body>
<a href="${tPath}/allMailServlet?page=1">查看邮件信息</a>
</body>
</html>
<%@page import="java.URLDecoder"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>登录查询界⾯</title>
<style type="text/css">
#msg{
width:200px;
margin:10px auto;
}
</style>
</head>
<body>
<h3 >${msg}</h3>
<div id="check">
<div id="3">
<c:forEach items="${requestScope.maillist}" var="mail">
<a href="${tPath}/DetailServlet?id=${mail.id}">${mail.name}</a>  <br/><br/>
</c:forEach>
<a href="${tPath}/allMailServlet?page=1">⾸页</a>
<a href="${tPath}/allMailServlet?page=${requestScope.currentPage-1}">上⼀页</a>          <a href="${tPath}/allMailServlet?page=${requestScope.currentPage+1}">下⼀页</a>          <a href="${tPath}/allMailServlet?page=${alPage}">末页</a>
</div>
</div>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。