简单的分页显示工具类

<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<link href="css/style.css" type="text/css" rel="stylesheet">
<%@ page import="java.sql.*"%>
<%
request.setCharacterEncoding("UTF-8") ;
%>
<%! // 定义全局常量,定义数据库的连接信息
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
public static final String USER = "root" ;
public static final String PASSWORD = "mysqladmin" ;
%>
<% // 准备JDBC操作要使用的相关对象
Connection conn ;
PreparedStatement pstmt ;
ResultSet rs ;
%>
<%
String dept_list_url = "dept_list_d.jsp" ;
long currentPage = 1L ; // 当前所在页
int lineSize = 1 ; // 每页显示的数据行
long allRecorders = 0 ; // 总记录数 
long allPages = 1 ; // 表示总页数
String columnData = "部门名称:dname|部门位置:loc" ; // 定义搜索列的名称


String column = request.getParameter("col") ; // 查询列
String keyWord = request.getParameter("kw") ; // 关键字
try { // 这行代码出错只有不传递或传递非法参数的时候出现
currentPage = Long.parseLong(request.getParameter("cp")) ;
} catch (Exception e) {}
try { // 如果出错就使用默认值
lineSize = Integer.parseInt(request.getParameter("ls")) ;
} catch (Exception e) {}
%>
<%
Class.forName(DBDRIVER) ; // 向容器之中加载数据库驱动程序
conn = DriverManager.getConnection(DBURL,USER,PASSWORD) ; // 连接数据库
String sql = null ;
if (column == null || "".equals(column) || keyWord == null || "".equals(keyWord)) {
sql = "SELECT COUNT(*) FROM dept " ; // 总的记录数
pstmt = conn.prepareStatement(sql) ;
} else {
sql = "SELECT COUNT(*) FROM dept WHERE " + column + " LIKE ?" ; // 总的记录数
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,"%"+keyWord+"%") ;
}
rs = pstmt.executeQuery() ;
if (rs.next()) { // 接收数据
allRecorders = rs.getLong(1) ;
}
if (allRecorders > 0) { // 有记录
allPages = (allRecorders + lineSize - 1) / lineSize ;
}
if (column == null || "".equals(column) || keyWord == null || "".equals(keyWord)) {
sql = "SELECT deptno,dname,loc FROM dept LIMIT ?,?" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setLong(1,(currentPage - 1) * lineSize) ;
pstmt.setLong(2,lineSize) ;
} else { // 设置查询列
sql = "SELECT deptno,dname,loc FROM dept WHERE " + column + " LIKE ? LIMIT ?,?" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,"%"+keyWord+"%") ;
pstmt.setLong(2,(currentPage - 1) * lineSize) ;
pstmt.setLong(3,lineSize) ;
}

rs = pstmt.executeQuery() ; // 读取数据
%>
<div id="searchPageDiv" align="center">
<form action="<%=dept_list_url%>" method="post">
<select id="col" name="col">
<%
String columnResults [] = columnData.split("\\|") ;
for (int x = 0 ; x < columnResults.length ; x ++) {
String temp [] = columnResults[x].split(":") ;
%>
<option value="<%=temp[1]%>" <%=temp[1].equals(column) ? "selected" : ""%>><%=temp[0]%></option>
<%
}
%>
</select>
<input type="text" id="kw" name="kw" value="<%=keyWord == null ? "" : keyWord%>">
<input type="submit" value="检索">
</form>
</div>
<h1>column = <%=column%>、keyWord = <%=keyWord%></h1>
<table border="1" width="100%">
<thead>
<tr>
<td>部门编号</td>
<td>部门名称</td>
<td>部门地址</td>
</tr>
</thead>
<tbody>
<%
while (rs.next()) {
long deptno = rs.getLong(1) ;
String dname = rs.getString(2) ;
String loc = rs.getString(3) ;
%>
<tr>
<td><%=deptno%></td>
<td><%=dname%></td>
<td><%=loc%></td>
</tr>
<%
}
%>
</tbody>
</table>
<%
conn.close() ;
%>
<%
int seed = 3 ; // 设置一个种子数
%>
<%
if (keyWord == null) {
keyWord = "" ;
}
%>
<div id="splitControlDiv" style="float:right">
<ul class="pagination"> 
<li class="<%=currentPage == 1 ? "active" : ""%>"><a href="<%=dept_list_url%>?cp=1&ls=<%=lineSize%>&col=<%=column%>&kw=<%=keyWord%>">1</a></li>
<%
if (allPages >= seed * 2 ) { // 表示后面的页数已经超过了5页
if (currentPage < seed * 2 - 1) {
for (int x = 2 ; x < seed * 2 ; x ++) {
%>
<li class="<%=currentPage == x ? "active" : ""%>"><a href="<%=dept_list_url%>?cp=<%=x%>&ls=<%=lineSize%>&col=<%=column%>&kw=<%=keyWord%>"><%=x%></a></li>
<%
}
} else {
if (currentPage > seed * 2 - 1) {
%>
<li class="disabled"><span>...</span></li>
<%
}
long startPage = currentPage - seed ;
long endPage = currentPage + seed ;
if (endPage >= allPages) { // 计算后的结果大于了总页数
endPage = allPages - 1 ;
}
for (long x = startPage ; x <= endPage ; x ++) {
%>
<li class="<%=currentPage == x ? "active" : ""%>"><a href="<%=dept_list_url%>?cp=<%=x%>&ls=<%=lineSize%>&col=<%=column%>&kw=<%=keyWord%>"><%=x%></a></li>
<%
}
}
if ((currentPage + seed + 1) < allPages) {
%>
<li class="disabled"><span>...</span></li>
<%
}
} else { // 页数不多,可以一次性全部显示
for (int x = 2 ; x < allPages ; x ++) {
%>
<li class="<%=currentPage == x ? "active" : ""%>"><a href="<%=dept_list_url%>?cp=<%=x%>&ls=<%=lineSize%>&col=<%=column%>&kw=<%=keyWord%>"><%=x%></a></li>
<%
}
}
%>
<%
if (allPages > 1) {
%>
<li class="<%=currentPage == allPages ? "active" : ""%>"><a href="<%=dept_list_url%>?cp=<%=allPages%>&ls=<%=lineSize%>&col=<%=column%>&kw=<%=keyWord%>"><%=allPages%></a></li>
<%
}
%>
</ul><br>
总记录数:<%=allRecorders%>、总页数:<%=allPages%>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值