1个回答
展开全部
这是我写的分页代码。不是最好的,但是能实现:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,com.mu.tool.*" errorPage="" %>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">
<!--
body {
background-image: url(img/bulletin/box_bg.jpg);
}
-->
</style></head>
<LINK
href="css/div.css" type=text/css rel=stylesheet>
<SCRIPT language=javaScript src="css/global.js"
type=text/javascript></SCRIPT>
<style type="text/css">
<!--
.STYLE5 {font-family: "宋体";
font-size: 15px;
color: #FFFFFF;
font-weight: bold;
}
.STYLE5 {color: #FFFFFF}
.STYLE6 { color: #FFFFFF;
font-weight: bold;
}
-->
</style>
<body>
<%
//每页几行
int pagecount = 5;
//共几页
int count = 0;
//当前页码
int pagenum = 1;
//判断是否第一次访问本页,是则显示第一页,否则显示用户点的页
if(request.getParameter("pages")!=null){
pagenum = Integer.parseInt(request.getParameter("pages"));
}
//求出一共有多少页
PreparedStatement co = DBAccesser.getPS("select count(*) from bulletin");
ResultSet rse = co.executeQuery();
if(rse.next()){
if(rse.getInt(1)%pagecount==0){
count = rse.getInt(1)/pagecount;
}else{
count = (rse.getInt(1)/pagecount)+1;
}
}
rse.close();
co.close();
//判断页码是否超出页面最大数
if(pagenum>count){
out.print("公告已作最新调整,请刷新后重新浏览!");
return;
}
//算分页的数据
int topA = (pagenum-1)*pagecount;
//根据页码访问数据库,抽取相应数据
PreparedStatement ps = DBAccesser.getPS("select top "+pagecount+" * from bulletin where id not in (select top "+topA+" id from bulletin order by issue_date DESC)order by issue_date DESC");
ResultSet rs = ps.executeQuery();
//格式化显示在页面上
while(rs.next()){
out.print("<br>");
out.print(rs.getString(3).substring(0,10)+" 【"+rs.getString(4)+"】"+" "+rs.getString(2)+"<br><DIV class=line0><IMG height=1></DIV>");
}
out.print("<br>");
//打印页码
out.print("页码 ");
for(int i=1;i<=count;i++){
if(i==pagenum){
out.print(i);
}else{
out.print(" <a href=bulletin_fen.jsp?pages="+i+">"+i+"</a> ");
}
}
%>
</body>
</html>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,com.mu.tool.*" errorPage="" %>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">
<!--
body {
background-image: url(img/bulletin/box_bg.jpg);
}
-->
</style></head>
<LINK
href="css/div.css" type=text/css rel=stylesheet>
<SCRIPT language=javaScript src="css/global.js"
type=text/javascript></SCRIPT>
<style type="text/css">
<!--
.STYLE5 {font-family: "宋体";
font-size: 15px;
color: #FFFFFF;
font-weight: bold;
}
.STYLE5 {color: #FFFFFF}
.STYLE6 { color: #FFFFFF;
font-weight: bold;
}
-->
</style>
<body>
<%
//每页几行
int pagecount = 5;
//共几页
int count = 0;
//当前页码
int pagenum = 1;
//判断是否第一次访问本页,是则显示第一页,否则显示用户点的页
if(request.getParameter("pages")!=null){
pagenum = Integer.parseInt(request.getParameter("pages"));
}
//求出一共有多少页
PreparedStatement co = DBAccesser.getPS("select count(*) from bulletin");
ResultSet rse = co.executeQuery();
if(rse.next()){
if(rse.getInt(1)%pagecount==0){
count = rse.getInt(1)/pagecount;
}else{
count = (rse.getInt(1)/pagecount)+1;
}
}
rse.close();
co.close();
//判断页码是否超出页面最大数
if(pagenum>count){
out.print("公告已作最新调整,请刷新后重新浏览!");
return;
}
//算分页的数据
int topA = (pagenum-1)*pagecount;
//根据页码访问数据库,抽取相应数据
PreparedStatement ps = DBAccesser.getPS("select top "+pagecount+" * from bulletin where id not in (select top "+topA+" id from bulletin order by issue_date DESC)order by issue_date DESC");
ResultSet rs = ps.executeQuery();
//格式化显示在页面上
while(rs.next()){
out.print("<br>");
out.print(rs.getString(3).substring(0,10)+" 【"+rs.getString(4)+"】"+" "+rs.getString(2)+"<br><DIV class=line0><IMG height=1></DIV>");
}
out.print("<br>");
//打印页码
out.print("页码 ");
for(int i=1;i<=count;i++){
if(i==pagenum){
out.print(i);
}else{
out.print(" <a href=bulletin_fen.jsp?pages="+i+">"+i+"</a> ");
}
}
%>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询