Access数据库实现分页查询Access数据库实现分页查询
resultset 遍历⽬录
说明
项⽬环境:Eclipse+Tomcat服务器+Access数据库+servlet+jdbc驱动
项⽬的结构
图⽚:
SQL语句
分页查的SQL语句,代码⽚.
String sql="select top 4 * from Histrjn where JourNo not in "
+"(select top "+(n-1)*4+" JourNo from Histrjn order by JourNo ) "
+"order by JourNo ";
//top:sql语句关键字
//4:分页中⼀页显⽰的记录条数
//Histrjn:表名
//JourNo:主键属性
//n:当前页⾯数
/
/not in:不在其中
//order by JourNo:对主键属性排序
说明:
top是分页sql语句的关键字
提醒:
1. 第⼀页时n=1,sql中(n-1)是0
Servlet:page.java
page.java来进⾏数据库分页查询,代码⽚.
package hello;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//使⽤注解定义Servlet
@WebServlet("/page")
public class page extends HttpServlet {
private static final long serialVersionUID =1L;
Connection con;
Statement st;
ResultSet rs;
public page(){
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
Class.forName("com.hxtt.sql.access.AccessDriver");// jdbc数据库驱动
String url ="jdbc:Access:///F:HrMS.accdb";// 数据源名称
Connection con = Connection(url,"","");// 建⽴数据库连接对象
Statement st = ateStatement();
/
/ 获得Histrjn表中记录的总数
ResultSet rs = st.executeQuery("select count(JourNo) from Histrjn");
<();
int x = rs.getInt(1);
System.out.println("总记录数:"+ x);
// 判断是否被平均分页,确定分页总数
if(x %4==0){
x = x /4;
}else{
x = x /4+1;
}
request.setAttribute("x", x);// 分页总数加⼊required中
int n =1;// 初始化当前页⾯数
Parameter("n")==null){
n =1;
}else{
// 从页⾯中获取请求页数
n = Integer.Parameter("n"));
}
// 分页查询的sql语句
String sql ="select top 4 * from Histrjn where JourNo not in "+"(select top "+(n -1)*4 +" JourNo from Histrjn order by JourNo ) "+"order by JourNo ";
rs = st.executeQuery(sql);//
List<Map<String, Object>> list = vertList(rs);
// 检验list对象,打印list内容,⾮必要
page.viewList(list);
// 获得表属性字段名
Set<String> key = (0).keySet();
request.setAttribute("key", key);
request.setAttribute("list", list);
}catch(ClassNotFoundException | SQLException e){
e.printStackTrace();
}finally{
if(rs !=null|| st !=null|| con !=null){
try{
rs.close();
st.close();
con.close();
}catch(SQLException e1){
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
// 将ResultSet对象转为List<Map<String, Object>>
public static List<Map<String, Object>>convertList(ResultSet rs) throws SQLException {  List<Map<String, Object>> list =new LinkedList<Map<String, Object>>();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
()){
// 第⼀个插⼊的为第⼀个map值对
Map<String, Object> rowData =new LinkedHashMap<String, Object>();
for(int i =1; i <= columnCount; i++){
rowData.ColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
return list;
}
// 检验list对象,显⽰list,⾮必要
public static void viewList(List<Map<String, Object>> list){
// 遍历list
Iterator<Map<String, Object>> li = list.iterator();
while(li.hasNext()){
// 遍历map
for(Map.Entry<String, Object> ma : li.next().entrySet()){
System.out.Value()+"  ");
}
System.out.println("");
}
}
}
⽅法public static void viewList(List<Map<String, Object>> list)是⾮必要的
提醒:
在map中属性的排列和数据库的顺序不同,因为map对象按照key的⼤⼩进⾏了排序
显⽰页⾯query.jsp
显⽰分页查询的页⾯query.jsp,使⽤了< c:forEach > 进⾏遍历,代码⽚.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="java.sun/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"> <html>
<head>
<title>显⽰页⾯</title>
<style type="text/css">
td {
text-align: middle;
text-align: center;
font-size:25px;
}
</style>
</head>
<body>
<br>
<br>
<br>
<table align="center" border="1" width="1000" height="170"
cellpadding="0" cellspacing="0">
<tr>
<!--遍历集合对象-->
<c:forEach var="ke" items="${key}">
<td>${ke}</td>
</c:forEach>
</tr>
<!--嵌套遍历集合对象-->
<c:forEach var="map" items="${list}">
<tr>
<c:forEach var="me" items="${map}">
<td>${me.value}</td>
</c:forEach>
</tr>
</c:forEach>
</table>
<br>
<br>
<div align="center">
<font size="5">第
<!--循环遍历-->
<c:forEach begin="1" end="${x}"var="i">
<a href="page?n=${i}">${i}</a>
</c:forEach>页
</font>
</div>
</body>
</html>
访问页⾯hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>分页查询访问页⾯</title>
</head>
<body>
<a href="page">分页查询</a>
</body>
</html>
提醒:
该页⾯没有设置编码⽅式。
总结
1. 使⽤注解注册servlet,不需要对l进⾏配置了,使⽤默认的就⾏
2. 分页查询时如果id不连续,分页效果则不能完成
**引⽤包是最⼤的困难(⼀步⼀步来)**

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