关于JSP分页的问题

glys2050 2010-10-18 11:31:50
[size=11px][size=10px]参照网上给的示例写了一个JSP的分页,但是发现每页出来的都是同样那几条记录,而且翻页的时候速度也很慢,搞不清是哪儿的问题,请高人指点啊。
整页的代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="connDb" scope="page" class="db_smile.db_smile" />
<jsp:useBean id="connDb1" scope="page" class="db_smile.db_smile" />
<jsp:useBean id="connPage" scope="page" class="db_smile.db_smile" />

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>优秀案例评分</title>
<style type="text/css">
<!--
.STYLE4 {font-size: 12px}
.STYLE6 {font-size: 14px; font-weight: bold; }
.STYLE7 {color: #0066FF}
.STYLE9 {
font-size: 24px;
font-weight: bold;
color: #993399;
}
.STYLE10 {font-family: "华文彩云"}
-->
</style>
</head>

<body background="../resources/default/images/bg_bluegreen.jpg">
<form id="form1" name="form1" method="post" action="viewJqm.jsp">
<table width="100%" border="1">
<tr bgcolor="#99CCFF">
<td height="141"><div align="center"><img src="../login_clip_image001_0001.gif" width="545" height="101" /></div></td>
</tr>
<%
request.setCharacterEncoding("gb2312");
String user = "";

if((String)session.getAttribute("user") == null)
{
out.println("<script language = javascript>alert('请先登录!');window.location.href = '../login.jsp';</script>");

}
else
{
user = (String)session.getAttribute("user");
byte b[] = user.getBytes("ISO-8859-1");
user = new String(b);
}
%>
<tr bgcolor="#CCCCCC">
<td height="34"><div align="right"><span class="STYLE4"><strong><span class="STYLE7">欢迎您,</span></strong><font color="#9933CC"><strong> <%=user%></strong></font> </span>
<!--
<input name="button2" type="button" id="button2" value="返回首页" onclick="window.location.href='../viewIndex.jsp'" />
 
-->
<input name="button" type="button" id="button" value="注 销" onclick="window.location.href='../logout.jsp'" />
</div></td>
</tr>
<tr bgcolor="#0099FF">
<td height="47"><span class="STYLE9">  <span class="STYLE10">优秀案例评分</span></span></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="92"><table width="100%" border="1">
<tr bordercolor="#0066FF">
<td height="71"><table width="100%" border="1">
<tr bordercolor="#0033FF" bgcolor="#99CCFF">
<td width="7%" bgcolor="#99CCFF"><div align="center" class="STYLE6">序号</div></td>
<td width="13%"><div align="center" class="STYLE6">流水号</div></td>
<td width="9%" height="33"><div align="center" class="STYLE6">受理日期</div></td>
<td width="8%"><div align="center" class="STYLE6">来电原因</div></td>
<td width="6%"><div align="center" class="STYLE6">受理人工号</div></td>
<!--
<td><div align="center" class="STYLE6">受理人姓名</div></td>
<td><div align="center"><span class="STYLE6">所属班组</span></div></td>
<td><div align="center"><span class="STYLE6">所属团队</span></div></td>
-->
<td width="19%"><div align="center" class="STYLE6">班长点评</div></td>
<td width="9%"><div align="center"><span class="STYLE6">案例类型</span></div></td>
<td width="7%"><div align="center"><span class="STYLE6">提交日期</span></div></td>
<td width="6%"><div align="center"><span class="STYLE6">提交人</span></div></td>
<td width="5%"><div align="center"><span class="STYLE6"> </span></div></td>
<td width="4%"><div align="center"><span class="STYLE6">总分</span></div></td>
</tr>
<%
//分页
int j;
int ipage = 20; //分页单位
int allpage; //总页数
int pages; //接受的页码变量
int cpage = 1; //当前页
int spage; //开始页

String pagesql = "select count(流水号) as cr from radio where [是否提交] = '是'";
//取总记录数
ResultSet pagers = connPage.executeQuery(pagesql);
int allCol = 0;
if(pagers.next())
allCol = pagers.getInt("cr");
//取总页数
allpage = (int)Math.ceil(allCol + ipage-1)/ipage;

if(request.getParameter("pages") == null)
{
pages = 1;
}
else{
pages = Integer.parseInt(request.getParameter("pages"));
}
//判断当前页
if(pages > allpage || pages == 0)
{
cpage = 1;
}
else
{
cpage = pages;
}
//判断起始页
spage = (cpage-1)*ipage;

//取查询记录集
String sql = "";
//sql = "select r.*,u.* from radio as r,user as u where r.是否提交 <> '是' and u.user = r.受理工号 and u.class in (select class from radio where user = '"+user+"')";
//sql = "select top "+ipage+" * from radio where [是否提交] = '是' and [流水号] not in (select top "+(cpage-1)*ipage+" [流水号] from radio order by [提交日期],[流水号])order by [提交日期],[流水号]";
if(cpage == 1)
sql = "select top "+ipage+" * from radio where [是否提交] = '是' order by [提交日期],[流水号]";
else
sql = "select Top "+ipage+" * from radio where [是否提交] = '是' and [流水号] not in (select top "+spage+" [流水号] from radio order by [提交日期],[流水号]) order by [提交日期],[流水号] ";
out.println(sql);
boolean flag = false;

String strSn = "";
ResultSet rs = connDb.executeQuery(sql);
int i = 0;
while(rs.next())
{
flag = true;
strSn = rs.getString("流水号");
String isSubmit = rs.getString("是否提交");
i++;

%>
<tr bordercolor="#0033FF" bgcolor="#99FFFF">
<td><div align="center" class="STYLE4"><%=i%>
</div></td>
<td><div align="center" class="STYLE4"><a href="viewRdetail.jsp?sn=<%=strSn%>"><%=strSn%></a></div></td>
<td><div align="center">
<div align="center" class="STYLE4"><%=rs.getString("受理日期")%></div></td>
<td><div align="center" class="STYLE4"><%=rs.getString("来电原因")%></div></td>
<td><div align="center">
<div align="center" class="STYLE4"><%=rs.getString("受理工号")%></div></td>

<!--
<td><div align="center">
<div align="center" class="STYLE4">姓名</div></td>
<td><div align="center" class="STYLE4">班组</div></td>
<td><div align="center" class="STYLE4">团队</div></td>
-->
<td><div align="center">
<div align="center" class="STYLE4"><%=rs.getString("班长点评")%></div></td>
<td><div align="center" class="STYLE4"><%=rs.getString("案例类型")%></div></td>
<td><div align="center" class="STYLE4"><%=rs.getString("提交日期")%></div></td>

<%

String sql1 = "select taidu+jiejue+zhudong+fgoutong+fchaxun+fpoint+yqingxu+yjiqiao as 总分,subPerson from remark where rsn = '"+strSn+"'";
ResultSet rs1 = connDb1.executeQuery(sql1);
if(rs1.next())
{
%>
<td><div align="center" class="STYLE4"><%=rs1.getString("subPerson")%></div></td>
<td>
<div align="center" class="STYLE4">
<strong>已评分</strong></div> </td>
<td><div align="center" class="STYLE4"> <strong><%=rs1.getString("总分")%></strong></div></td>

<%
}
else
{
%>
<td width="1%"><div align="center" class="STYLE4"><strong></strong></div> </td>
<td width="6%">

<div align="center" class="STYLE4">
<a href="AddRemark.jsp?sn=<%=strSn%>&no=<%=i%>"><strong>评 分</strong></a> </div> </td>
<%
}

rs1.close();
rs1 = null;
connDb1.closeCon();
}
sql = "";
rs.close();
rs = null;
connDb.closeCon();

if(!flag)
{
out.println("<font color = red><strong>很抱歉,查询不到相关信息!请确认您的班组是否有优秀案例已导入系统。</strong></font>");
}
%>
</tr>
<tr bordercolor="#0033FF" bgcolor="#99CCFF">
<td colspan="13" align="right" valign="middle">共<%=allCol%>条记录 共<%=allpage%>页
<%
if(cpage > 1)
{
%>
<a href="testPages.jsp?pages=<%=cpage-1%>"></a>
<%
}
if(cpage < allpage)
{
%>
<a href="testPages.jsp?pages=<%=cpage+1%>"></a>
<%
}
for(j = 1; j <= allpage; j++)
{
if(j != pages)
{
%>
<a href="testPages.jsp?pages=<%=j%>"><%=j%></a> 
<%
}
}
%></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
[/size][/size]
...全文
143 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jay_+wqq_635731323 2010-10-21
  • 打赏
  • 举报
回复
还是用组件吧
xiaoH1314 2010-10-21
  • 打赏
  • 举报
回复
太长了 用sql和Java类比较简单些
gdstcyhp 2010-10-20
  • 打赏
  • 举报
回复
学习了
smallcol1 2010-10-20
  • 打赏
  • 举报
回复
用分页数据库语句,先把数据全部读到一个集合中,
再从集合中把数据读到表格中
当然数据库表要设置主键或索引,再就是你数据库查询语句的优化,分页时尽量不要使用not in之类的查询条件
st_linxiaoling 2010-10-20
  • 打赏
  • 举报
回复
写sql语句分好再读取让它显示
shixitong 2010-10-19
  • 打赏
  • 举报
回复
太长了,现在很少这么做了!(jsp代码中很少插入java代码了)
首先你确定你数据库中是不是只有这几条数据,然后判断各个参数是否每次都正确!
shixitong 2010-10-19
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="connDb" scope="page" class="db_smile.db_smile" />
<jsp:useBean id="connDb1" scope="page" class="db_smile.db_smile" />
<jsp:useBean id="connPage" scope="page" class="db_smile.db_smile" />

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>优秀案例评分</title>
<style type="text/css">
<!--
.STYLE4 {font-size: 12px}
.STYLE6 {font-size: 14px; font-weight: bold; }
.STYLE7 {color: #0066FF}
.STYLE9 {
font-size: 24px;
font-weight: bold;
color: #993399;
}
.STYLE10 {font-family: "华文彩云"}
-->
</style>
</head>

<body background="../resources/default/images/bg_bluegreen.jpg">
<form id="form1" name="form1" method="post" action="viewJqm.jsp">
<table width="100%" border="1">
<tr bgcolor="#99CCFF">
<td height="141"><div align="center"><img src="../login_clip_image001_0001.gif" width="545" height="101" /></div></td>
</tr>
<%
request.setCharacterEncoding("gb2312");
String user = "";

if((String)session.getAttribute("user") == null)
{
out.println("<script language = javascript>alert('请先登录!');window.location.href = '../login.jsp';</script>");

}
else
{
user = (String)session.getAttribute("user");
byte b[] = user.getBytes("ISO-8859-1");
user = new String(b);
}
%>
<tr bgcolor="#CCCCCC">
<td height="34"><div align="right"><span class="STYLE4"><strong><span class="STYLE7">欢迎您,</span></strong><font color="#9933CC"><strong> <%=user%></strong></font> </span>
<!--
<input name="button2" type="button" id="button2" value="返回首页" onclick="window.location.href='../viewIndex.jsp'" />
 
-->
<input name="button" type="button" id="button" value="注 销" onclick="window.location.href='../logout.jsp'" />
</div></td>
</tr>
<tr bgcolor="#0099FF">
<td height="47"><span class="STYLE9">  <span class="STYLE10">优秀案例评分</span></span></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="92"><table width="100%" border="1">
<tr bordercolor="#0066FF">
<td height="71"><table width="100%" border="1">
<tr bordercolor="#0033FF" bgcolor="#99CCFF">
<td width="7%" bgcolor="#99CCFF"><div align="center" class="STYLE6">序号</div></td>
<td width="13%"><div align="center" class="STYLE6">流水号</div></td>
<td width="9%" height="33"><div align="center" class="STYLE6">受理日期</div></td>
<td width="8%"><div align="center" class="STYLE6">来电原因</div></td>
<td width="6%"><div align="center" class="STYLE6">受理人工号</div></td>
<!--
<td><div align="center" class="STYLE6">受理人姓名</div></td>
<td><div align="center"><span class="STYLE6">所属班组</span></div></td>
<td><div align="center"><span class="STYLE6">所属团队</span></div></td>
-->
<td width="19%"><div align="center" class="STYLE6">班长点评</div></td>
<td width="9%"><div align="center"><span class="STYLE6">案例类型</span></div></td>
<td width="7%"><div align="center"><span class="STYLE6">提交日期</span></div></td>
<td width="6%"><div align="center"><span class="STYLE6">提交人</span></div></td>
<td width="5%"><div align="center"><span class="STYLE6"> </span></div></td>
<td width="4%"><div align="center"><span class="STYLE6">总分</span></div></td>
</tr>
<%
//分页
int j;
int ipage = 20; //分页单位
int allpage; //总页数
int pages; //接受的页码变量
int cpage = 1; //当前页
int spage; //开始页

String pagesql = "select count(流水号) as cr from radio where [是否提交] = '是'";
//取总记录数
ResultSet pagers = connPage.executeQuery(pagesql);
int allCol = 0;
if(pagers.next())
allCol = pagers.getInt("cr");
//取总页数
allpage = (int)Math.ceil(allCol + ipage-1)/ipage;

if(request.getParameter("pages") == null)
{
pages = 1;
}
else{
pages = Integer.parseInt(request.getParameter("pages"));
}
//判断当前页
if(pages > allpage || pages == 0)
{
cpage = 1;
}
else
{
cpage = pages;
}
//判断起始页
spage = (cpage-1)*ipage;

//取查询记录集
String sql = "";
//sql = "select r.*,u.* from radio as r,user as u where r.是否提交 <> '是' and u.user = r.受理工号 and u.class in (select class from radio where user = '"+user+"')";
//sql = "select top "+ipage+" * from radio where [是否提交] = '是' and [流水号] not in (select top "+(cpage-1)*ipage+" [流水号] from radio order by [提交日期],[流水号])order by [提交日期],[流水号]";
if(cpage == 1)
sql = "select top "+ipage+" * from radio where [是否提交] = '是' order by [提交日期],[流水号]";
else
sql = "select Top "+ipage+" * from radio where [是否提交] = '是' and [流水号] not in (select top "+spage+" [流水号] from radio order by [提交日期],[流水号]) order by [提交日期],[流水号] ";
out.println(sql);
boolean flag = false;

String strSn = "";
ResultSet rs = connDb.executeQuery(sql);
int i = 0;
while(rs.next())
{
flag = true;
strSn = rs.getString("流水号");
String isSubmit = rs.getString("是否提交");
i++;

%>
<tr bordercolor="#0033FF" bgcolor="#99FFFF">
<td><div align="center" class="STYLE4"><%=i%>
</div></td>
<td><div align="center" class="STYLE4"><a href="viewRdetail.jsp?sn=<%=strSn%>"><%=strSn%></a></div></td>
<td><div align="center">
<div align="center" class="STYLE4"><%=rs.getString("受理日期")%></div></td>
<td><div align="center" class="STYLE4"><%=rs.getString("来电原因")%></div></td>
<td><div align="center">
<div align="center" class="STYLE4"><%=rs.getString("受理工号")%></div></td>

<!--
<td><div align="center">
<div align="center" class="STYLE4">姓名</div></td>
<td><div align="center" class="STYLE4">班组</div></td>
<td><div align="center" class="STYLE4">团队</div></td>
-->
<td><div align="center">
<div align="center" class="STYLE4"><%=rs.getString("班长点评")%></div></td>
<td><div align="center" class="STYLE4"><%=rs.getString("案例类型")%></div></td>
<td><div align="center" class="STYLE4"><%=rs.getString("提交日期")%></div></td>

<%

String sql1 = "select taidu+jiejue+zhudong+fgoutong+fchaxun+fpoint+yqingxu+yjiqiao as 总分,subPerson from remark where rsn = '"+strSn+"'";
ResultSet rs1 = connDb1.executeQuery(sql1);
if(rs1.next())
{
%>
<td><div align="center" class="STYLE4"><%=rs1.getString("subPerson")%></div></td>
<td>
<div align="center" class="STYLE4">
<strong>已评分</strong></div> </td>
<td><div align="center" class="STYLE4"> <strong><%=rs1.getString("总分")%></strong></div>& lt;/td>

<%
}
else
{
%>
<td width="1%"><div align="center" class="STYLE4"><strong></strong></div> </td>
<td width="6%">

<div align="center" class="STYLE4">
<a href="AddRemark.jsp?sn=<%=strSn%>&no=<%=i%>">< strong>评 分</strong></a> </div> </td>
<%
}

rs1.close();
rs1 = null;
connDb1.closeCon();
}
sql = "";
rs.close();
rs = null;
connDb.closeCon();

if(!flag)
{
out.println("<font color = red><strong>很抱歉,查询不到相关信息!请确认您的班组是否有优秀案例已导入系统。</strong></font>");
}
%>
</tr>
<tr bordercolor="#0033FF" bgcolor="#99CCFF">
<td colspan="13" align="right" valign="middle">共<%=allCol%>条记录 共<%=allpage%>页
<%
if(cpage > 1)
{
%>
<a href="testPages.jsp?pages=<%=cpage-1%>"></a>
<%
}
if(cpage < allpage)
{
%>
<a href="testPages.jsp?pages=<%=cpage+1%>"></a>
<%
}
for(j = 1; j <= allpage; j++)
{
if(j != pages)
{
%>
<a href="testPages.jsp?pages=<%=j%>"><%=j%></a> 
<%
}
}
%></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧