| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 465 人关注过本帖
标题:[求助为什么分页不能显示
只看楼主 加入收藏
zplove
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:783
专家分:0
注 册:2006-7-30
结帖率:100%
收藏
 问题点数:0 回复次数:2 
[求助为什么分页不能显示

<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<html>
<%
int dipage=1;//当前页码数默认为1
String pages=request.getParameter("dipage");
if(pages==null)
{
pages="1";
}
try
{
dipage=Integer.parseInt(pages);
}
catch(Exception e)
{
dipage=1;
}
%>
<head>
<style type="text/css">
<!--
.STYLE1 {
font-family: "华文行楷";
font-size: 36px;
}
-->
</style>
</head>

<body>
//数据的连接
<%
Connection con;
Statement sql;
ResultSet rs;
request.setCharacterEncoding("gb2312");
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
out.print(e);
}con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/zp","root","zhang");
sql=con.createStatement();
rs=sql.executeQuery("select * from fenye");
while (rs.next()){%>
<form id="form1" name="form1" method="post" action="">

<%
int countRecord=0;//记录条数
int countPageRecord=0;//每页记录条数
int countPage=0;//总页数
countPageRecord=5;//每页5条记录,要设置每页记录条数就更改这个变量的值
//得到记录的条数
rs.last();
countRecord=rs.getRow();
//得到总页数
if(countRecord/countPageRecord==0)
countPage=countRecord/countPageRecord;
else
countPage=countRecord/countPageRecord+1;
//把记录指针移至当前页第一条记录之前
if((dipage-1)*countPageRecord==0)
rs.beforeFirst();
else
rs.absolute((dipage-1)*countPageRecord);
int i=0;
%>
<table width="518" height="215" border="1" align="center">
<tr>
<td colspan="4" align="center"><span class="STYLE1">下面是查询的结果</span></td>
</tr>
<tr>
<td width="123">ID</td>
<td width="126">姓名</td>
<td width="113">性别</td>
<td width="128">年龄</td>
</tr>


<% long id=rs.getLong(1);%>
<tr>
<td><%=id%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getLong(4)%></td>
</tr>
<%
i++;
if(i>=countPageRecord){
break; //当前页显示完,则退出循环
}else

out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+dipage+"页,每页"+countPageRecord+"条记录,");
if(dipage==1){
break;
}//当前是首页

else//当前不是首页
{
out.print("<a href=fenye.jsp?dipage=1>首页</a>,");
out.print("<a href=fenye.jsp?dipage="+(dipage-1)+">上一页</a>,");
}
if(dipage==countPage){
break;
}//当前是末页

else//当前不是末页
{
out.print("<a href=fenye.jsp?dipage="+(dipage+1)+">下一页</a>,");
out.print("<a href=fenye.jsp?dipage="+countPage+">末页</a>");
}


%>
<%}%>
<%
con.close();
%>
</table>

</form>
</body>
</html>

搜索更多相关主题的帖子: 华文行楷 import style null 
2006-08-04 12:22
ziyehanbin
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2005-10-16
收藏
得分:0 

我修改了一下你的分页程序!不过这样分页效率不高!如果能结合数据库本身的分页能力,就能得到很高的效率!当然还要照顾平台兼容性!!

-----------------------------------------------------------------------
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<style type="text/css">
<!--
.STYLE1 {
font-family: "华文行楷";
font-size: 36px;
}
-->
</style>
</head>

<body>
<%
try{
String receive_page=request.getParameter("dipage");
int dipage=1;//当前页码数默认为1
if(receive_page==null || receive_page.equals(""))
{
dipage=1;
}
else
{
dipage=Integer.parseInt(receive_page);
if(dipage<=0) dipage=1;
}

Connection con=null;
Statement sql;
ResultSet rs;
String dburl="jdbc:mysql://localhost:3306/bookDB?useUnicode=true&characterEncoding=GB2312";
String dbuser="root";
String dbpwd="831218";
request.setCharacterEncoding("gb2312");

Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(dburl,dbuser,dbpwd);
sql=con.createStatement();
rs=sql.executeQuery("select * from books");
int countRecord=0;//记录条数
int countPageRecord=0;//每页记录条数
int countPage=0;//总页数
countPageRecord=2;//每页5条记录,要设置每页记录条数就更改这个变量的值
rs.last();
countRecord=rs.getRow();
countPage=(countRecord+countPageRecord-1)/countPageRecord; //得到总页数

if (dipage>countPage) dipage=countPage;

if((dipage-1)*countPageRecord<=0){
rs.absolute(1);
rs.beforeFirst();
}
else
{
rs.absolute((dipage-1)*countPageRecord);

}
int i=0;
while (rs.next()){
%>
<%

%>
<table width="518" height="215" border="1" align="center">
<tr>
<td colspan="4" align="center"><span class="STYLE1">下面是查询的结果</span></td>
</tr>
<tr>
<td width="123">ID</td>
<td width="126">姓名</td>
<td width="113">性别</td>
<td width="128">年龄</td>
</tr>


<% String id=rs.getString(1);%>
<tr>
<td><%=id%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(6)%></td>
</tr>

<%

i++;
if(i>=countPageRecord){
break; //当前页显示完,则退出循环
}


} out.print("</table>");

out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+dipage+"页,每页"+countPageRecord+"条记录,");
out.print("<a href=test.jsp?dipage=1>首页</a>,");
out.print("<a href=test.jsp?dipage="+(dipage-1)+">上一页</a>,");
out.print("<a href=test.jsp?dipage="+(dipage+1)+">下一页</a>,");
out.print("<a href=test.jsp?dipage="+countPage+">末页</a>");


%>
<%
sql.close();
con.close();

}catch(ClassNotFoundException ex){
out.print(ex);
}
%>
</body>
</html>
----------------------------------------------------------------------

都是初学者,共同学习!!


2006-08-04 19:28
zplove
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:783
专家分:0
注 册:2006-7-30
收藏
得分:0 

谢谢你给俺的帮忙啊


相信自己的没错了
2006-08-04 19:53
快速回复:[求助为什么分页不能显示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014921 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved