1.使用MySql数据库建一个表,并添加记录:
-- 删除表
DROP TABLE person ;
-- 建立person表
CREATE TABLE person
(
-- 生成一个流水号,观察显示的记录数
id int AUTO_INCREMENT NOT NULL PRIMARY KEY ,
-- 用户的登陆ID
uid varchar(32) ,
-- 用户的真实姓名
name varchar(32) ,
-- 用户的登陆密码
password varchar(20)
) ;
-- 插入测试数据
INSERT INTO person(uid,name,password) VALUES ('LXH01','李兴华01','MLDN01') ;
INSERT INTO person(uid,name,password) VALUES ('LXH02','李兴华02','MLDN02') ;
INSERT INTO person(uid,name,password) VALUES ('LXH03','李兴华03','MLDN03') ;
INSERT INTO person(uid,name,password) VALUES ('LXH04','李兴华04','MLDN04') ;
INSERT INTO person(uid,name,password) VALUES ('LXH05','李兴华05','MLDN05') ;
INSERT INTO person(uid,name,password) VALUES ('LXH06','李兴华06','MLDN06') ;
INSERT INTO person(uid,name,password) VALUES ('LXH07','李兴华07','MLDN07') ;
INSERT INTO person(uid,name,password) VALUES ('LXH08','李兴华08','MLDN08') ;
INSERT INTO person(uid,name,password) VALUES ('LXH09','李兴华09','MLDN09') ;
INSERT INTO person(uid,name,password) VALUES ('LXH10','李兴华10','MLDN10') ;
INSERT INTO person(uid,name,password) VALUES ('LXH11','李兴华11','MLDN11') ;
INSERT INTO person(uid,name,password) VALUES ('LXH12','李兴华12','MLDN12') ;
INSERT INTO person(uid,name,password) VALUES ('LXH13','李兴华13','MLDN13') ;
INSERT INTO person(uid,name,password) VALUES ('LXH14','李兴华14','MLDN14') ;
INSERT INTO person(uid,name,password) VALUES ('LXH15','李兴华15','MLDN15') ;
INSERT INTO person(uid,name,password) VALUES ('LXH16','李兴华16','MLDN16') ;
INSERT INTO person(uid,name,password) VALUES ('LXH17','李兴华17','MLDN17') ;
INSERT INTO person(uid,name,password) VALUES ('LXH18','李兴华18','MLDN18') ;
INSERT INTO person(uid,name,password) VALUES ('LXH19','李兴华19','MLDN19') ;
INSERT INTO person(uid,name,password) VALUES ('LXH20','李兴华20','MLDN20') ;
INSERT INTO person(uid,name,password) VALUES ('LXH21','李兴华21','MLDN21') ;
INSERT INTO person(uid,name,password) VALUES ('LXH22','李兴华22','MLDN22') ;
INSERT INTO person(uid,name,password) VALUES ('LXH23','李兴华23','MLDN23') ;
2.下面一步步实现我们的分页显示,新建一张jsp页面,先实现基本的将所有数据显示在一张页面中:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>分页显示</title>
</head>
<body>
<center>
<h1>人员列表</h1>
<hr>
<br>
<%
final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
final String DBURL = "jdbc:mysql://localhost/mldn" ;
final String DBUSER = "root" ;
final String DBPASSWORD = "mysqladmin" ;
Connection conn = null ;
%>
<%
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
String sql = "SELECT id,uid,name,password FROM person" ;
PreparedStatement pstmt = null ;
pstmt = conn.prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery() ;
%>
<table border="1" width="80%">
<tr>
<td>编号</td>
<td>登陆名称</td>
<td>姓名</td>
<td>密码</td>
<td colspan="2">操作</td>
</tr>
<%
int i = 0 ;
while(rs.next())
{
i++ ;
int id = rs.getInt(1) ;
String userid = rs.getString(2) ;
String name = rs.getString(3) ;
String password = rs.getString(4) ;
%>
<tr>
<td><%=id%></td>
<td><%=userid%></td>
<td><%=name%></td>
<td><%=password%></td>
<td>更新</td>
<td>删除</td>
</tr>
<%
}
rs.close() ;
pstmt.close() ;
if(i==0)
{
%>
<tr>
<td colspan="6">没有任何数据!!</td>
</tr>
<%
}
%>
</table>
<%
}
catch(Exception e)
{
%>
<h2>系统出错!!!</h2>
<%
}
finally
{
conn.close() ;
}
%>
</center>
</body>
</html>
//变量i用于判断是否数据库中没有记录
将所有记录都显示在一张页面中,显然并不切实际
3.下面基于该代码逐步实现分页功能
a.定义一个变量记录每页显示的记录数,int lineSize;
b.定义一个变量记录当前的页数,int currentPage;
c.实现首页,上一页,下一页,尾页的按钮定义:
<script language="javaScript">
function openPage(curpage)
{
document.spage.cp.value = curpage ;
// alert(cupage) ;
document.spage.submit() ;
}
</script>
<form name="spage" action="<%=jspUrl%>">
<input type="button" value="首页" onClick="openPage(1)">
<input type="button" value="上一页" onClick="openPage(<%=currentPage-1%>)">
<input type="button" value="下一页" onClick="openPage(<%=currentPage+1%>)">
<input type="button" value="尾页">
<input type="hidden" name="cp" value="">
</form>
d.action中实现的是对本页做提交处理,所以在该页中应该获取一个参数cp,防止第一次传过来的是null所以抛出一个异常:
<%
// 接收传过来的当前页
try
{
currentPage = Integer.parseInt(request.getParameter("cp")) ;
}
catch(Exception e)
{}
%>
e.关键的是,对查询出的结果集的控制,每次最多取出lineSize个记录,在取出之前,先将前几页的记录空出:
<%
int i = 0 ;
for(int x=0;x<(currentPage-1)*lineSize;x++)
{
rs.next();
}
// 对于输出代码之前要求按显示的页数空出
for(int x=0;x<lineSize;x++)
{
if(rs.next())
{
i++ ;
int id = rs.getInt(1) ;
String userid = rs.getString(2) ;
String name = rs.getString(3) ;
String password = rs.getString(4) ;
%>
f.现在我们来实现对最后一页的显示,由于记录数不是固定的因此总页数需要计算得出,首先要算出总记录allRecorders
String sql = "SELECT COUNT(id) from person" ;
pstmt = conn.prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
allRecorders = rs.getInt(1) ;
}
rs.close() ;
pstmt.close() ;
// 计算总页数
pageSize = (allRecorders+lineSize-1)/lineSize ;
**********************************
尾页的动作改为:
<input type="button" value="尾页" onClick="openPage(<%=pageSize%>)">
g.到现在分页显示差不多完成了,还有一个问题就是,当翻至最后一页或第一页时再向后或者向前就会出现异常,解决方法是,在按钮中添加判断语句:
<input type="button" value="首页" onClick="openPage(1)" <%=currentPage==1?"disabled":""%>>
<input type="button" value="上一页" onClick="openPage(<%=currentPage-1%>)" <%=currentPage==1?"disabled":""%>>
<input type="button" value="下一页" onClick="openPage(<%=currentPage+1%>)" <%=currentPage==pageSize?"disabled":""%>>
<input type="button" value="尾页" onClick="openPage(<%=pageSize%>)" <%=currentPage==pageSize?"disabled":""%>>
<input type="hidden" name="cp" value="">
h.用下拉列表实现页间跳转:
跳转到
<select name="selpage" onChange="selOpenPage()">
<%
for(int x=1;x<=pageSize;x++)
{
%>
<option value="<%=x%>" <%=currentPage==x?"selected":""%>><%=x%></option>
<%
}
i.最后代码如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>分页显示</title>
</head>
<body>
<center>
<h1>人员列表</h1>
<hr>
<br>
<%!
final String jspUrl = "list_person_false_05.jsp" ;
%>
<%
// 定义如下分页变量
// 1、定义没页要显示的记录数
int lineSize = 10 ;
// 2、定义一个当前是第几页
int currentPage = 1 ;
// 计算出总页数
int pageSize = 0 ;
// 总记录数 / 每页显示的记录数
int allRecorders = 30 ;
%>
<%
// 接收传过来的当前页
try
{
currentPage = Integer.parseInt(request.getParameter("cp")) ;
}
catch(Exception e)
{}
%>
<%
final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
final String DBURL = "jdbc:mysql://localhost/mldn" ;
final String DBUSER = "root" ;
final String DBPASSWORD = "mysqladmin" ;
Connection conn = null ;
%>
<%
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
PreparedStatement pstmt = null ;
String sql = "SELECT COUNT(id) from person" ;
pstmt = conn.prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
allRecorders = rs.getInt(1) ;
}
rs.close() ;
pstmt.close() ;
// 计算总页数
pageSize = (allRecorders+lineSize-1)/lineSize ;
sql = "SELECT id,uid,name,password FROM person" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
%>
<script language="javaScript">
function openPage(curpage)
{
document.spage.cp.value = curpage ;
// alert(cupage) ;
document.spage.submit() ;
}
function selOpenPage()
{
document.spage.cp.value = document.spage.selpage.value ;
document.spage.submit() ;
}
</script>
<form name="spage" action="<%=jspUrl%>">
<input type="button" value="首页" onClick="openPage(1)" <%=currentPage==1?"disabled":""%>>
<input type="button" value="上一页" onClick="openPage(<%=currentPage-1%>)" <%=currentPage==1?"disabled":""%>>
<input type="button" value="下一页" onClick="openPage(<%=currentPage+1%>)" <%=currentPage==pageSize?"disabled":""%>>
<input type="button" value="尾页" onClick="openPage(<%=pageSize%>)" <%=currentPage==pageSize?"disabled":""%>>
<input type="hidden" name="cp" value="">
<font color="red" size="5"><%=currentPage%></font>
/
<font color="red" size="5"><%=pageSize%></font>
跳转到
<select name="selpage" onChange="selOpenPage()">
<%
for(int x=1;x<=pageSize;x++)
{
%>
<option value="<%=x%>" <%=currentPage==x?"selected":""%>><%=x%></option>
<%
}
%>
</select>
页
</form>
<table border="1" width="80%">
<tr>
<td>编号</td>
<td>登陆名称</td>
<td>姓名</td>
<td>密码</td>
<td colspan="2">操作</td>
</tr>
<%
int i = 0 ;
for(int x=0;x<(currentPage-1)*lineSize;x++)
{
rs.next();
}
// 对于输出代码之前要求按显示的页数空出
for(int x=0;x<lineSize;x++)
{
if(rs.next())
{
i++ ;
int id = rs.getInt(1) ;
String userid = rs.getString(2) ;
String name = rs.getString(3) ;
String password = rs.getString(4) ;
%>
<tr>
<td><%=id%></td>
<td><%=userid%></td>
<td><%=name%></td>
<td><%=password%></td>
<td>更新</td>
<td>删除</td>
</tr>
<%
}
}
rs.close() ;
pstmt.close() ;
if(i==0)
{
%>
<tr>
<td colspan="6">没有任何数据!!</td>
</tr>
<%
}
%>
</table>
<%
}
catch(Exception e)
{
%>
<h2>系统出错!!!</h2>
<%
}
finally
{
conn.close() ;
}
%>
</center>
</body>
</html>
注:代码由李兴华提供。
下载地址: