我的代码如下:
<%@ page language="java" contentType="text/html;charset=gb2312" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="shop.javabean.DBConnection" %>
<jsp:useBean id="db" class="shop.javabean.DBConnection" ></jsp:useBean>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/shop.css"/>
<title>店铺列表</title>
</head>
<body bgcolor="#ffffff"><table width="745" border="0" align="center">
<tr>
<td width="119" >店标</td>
<td width="251">店名</td>
<td width="83">主营产品</td>
<td width="63">访问人数</td>
<td width="63">开店日期</td>
</tr>
<%
String tdBgColor = "";
int num = 0;//控制单元格颜色
ResultSet rs = null;
String countRecord = "";
//sql2 = "select count(Remes_Id) from SReMessage where Mes_Id='"+Mes_Id+"' group by Mes_Id";
String sql0 = "select count(*) from Shop";
rs = db.executeQuery(sql0);
if(rs.next()){
countRecord = rs.getString(1);
}
String sql1 = "select Shop_Id,Shop_Name,Shop_Icon,Shop_MainPro,Shop_Visitors,Shop_OpenTime from Shop where Shop_State = 1 order by Shop_Visitors desc";
rs = db.executeQuery(sql1); int number = 1;
int intPageSize; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数 int intPage; //待显示的页码
String strPage;
int i;
intPageSize = 15; //设置一页的记录总数
strPage = request.getParameter("page"); //取得待显示的页码
if(strPage==null)
intPage = 1;
else
intPage=java.lang.Integer.parseInt(strPage); //将字符串转化为整型 if(intPage<1)
intPage = 1;
//rs.last(); // 获取记录总数
//intRowCount = rs.getRow();
intRowCount = Integer.parseInt(countRecord);
System.out.println("intRowCount="+intRowCount);
intPageCount = (intRowCount+intPageSize-1)/intPageSize;
if(intPage>intPageCount)
intPage = intPageCount;
if(intPageCount>0)
rs.absolute((intPage-1)*intPageSize+1); //将记录指针定位到待显示页的第一条记录上
i = 0;
try{
//以下部分为循环显示从数据库中读取出来的数据
while(i<intPageSize && !rs.isAfterLast()){
num++;
String Shop_Id = rs.getString("Shop_Id");
String Shop_Name = rs.getString("Shop_Name");//店名
String Shop_Icon = rs.getString("Shop_Icon");//店标
String Shop_MainPro = rs.getString("Shop_MainPro");//主营产品
String Shop_Visitors = rs.getString("Shop_Visitors");//访问人数
String Shop_OpenTime = rs.getString("Shop_OpenTime");//所在地
String time = Shop_OpenTime.substring(0,10);
System.out.println("ShopList.jspxxxxxxxxxShop_Id="+Shop_Id);
%>
<%
if(num%2==0){
tdBgColor="#FFFFF7";
}
else{
tdBgColor="#EEFAFF";
}
%>
<tr>
<td width="119" bgcolor="<%=tdBgColor %>"><a href="main.jsp?ShopId=<%=Shop_Id%>" target="_blank"><img src="<%=Shop_Icon %>" width="119" height="114" border="0" /></a></td>
<td width="251" bgcolor="<%=tdBgColor %>"><a href="main.jsp?ShopId=<%=Shop_Id%>" target="_blank"><%=Shop_Name %></a></td>
<td width="83" bgcolor="<%=tdBgColor %>"><%=Shop_MainPro %></td>
<td width="63" bgcolor="<%=tdBgColor %>"><%=Shop_Visitors %></td>
<td width="63" bgcolor="<%=tdBgColor %>"><%=time %></td>
</tr> <%
rs.next();
}}catch(Exception e){
e.printStackTrace();
}finally{
rs.close();
db.closeDb();
}
%>
</table><form name="pageForm" action="ShopList.jsp" method="post">
<table align="right" width="745">
<tr>
<td>
共<%= intRowCount %>条记录,第<%=intPage %>/<%=intPageCount %>页 <%if(intPage<2){ %> 首页 | 上一页
<% }else{%> <a href="ShopList.jsp?page=1">首页</a>|<a href="ShopList.jsp?page=<%=intPage-1%>&">上一页</a>
<%}%>
<%if((intPageCount-intPage)<1){ %> 下一页 | 尾页
<%}else{%>
<a href="ShopList.jsp?page=<%=intPage+1%>"> 下一页</a> | <a href="ShopList.jsp?page=<%=intPageCount%>">尾页</a>
<%}%>
转到第<input type="text" name="page" size="3"/>页<input type="submit" value="Go" class="input1"/>
</td>
</tr>
</table>
</form>
</body>
</html>
数据库中有4条记录,每页显示15条,但是实际上只显示了第一条和最后一条记录出来;这是一种情况,还有一种情况就是设置每页显示1条记录,但是实际上却把所有的记录都罗列在首页。
不知道是什么原因,请高手指教!
<%@ page language="java" contentType="text/html;charset=gb2312" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="shop.javabean.DBConnection" %>
<jsp:useBean id="db" class="shop.javabean.DBConnection" ></jsp:useBean>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/shop.css"/>
<title>店铺列表</title>
</head>
<body bgcolor="#ffffff"><table width="745" border="0" align="center">
<tr>
<td width="119" >店标</td>
<td width="251">店名</td>
<td width="83">主营产品</td>
<td width="63">访问人数</td>
<td width="63">开店日期</td>
</tr>
<%
String tdBgColor = "";
int num = 0;//控制单元格颜色
ResultSet rs = null;
String countRecord = "";
//sql2 = "select count(Remes_Id) from SReMessage where Mes_Id='"+Mes_Id+"' group by Mes_Id";
String sql0 = "select count(*) from Shop";
rs = db.executeQuery(sql0);
if(rs.next()){
countRecord = rs.getString(1);
}
String sql1 = "select Shop_Id,Shop_Name,Shop_Icon,Shop_MainPro,Shop_Visitors,Shop_OpenTime from Shop where Shop_State = 1 order by Shop_Visitors desc";
rs = db.executeQuery(sql1); int number = 1;
int intPageSize; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数 int intPage; //待显示的页码
String strPage;
int i;
intPageSize = 15; //设置一页的记录总数
strPage = request.getParameter("page"); //取得待显示的页码
if(strPage==null)
intPage = 1;
else
intPage=java.lang.Integer.parseInt(strPage); //将字符串转化为整型 if(intPage<1)
intPage = 1;
//rs.last(); // 获取记录总数
//intRowCount = rs.getRow();
intRowCount = Integer.parseInt(countRecord);
System.out.println("intRowCount="+intRowCount);
intPageCount = (intRowCount+intPageSize-1)/intPageSize;
if(intPage>intPageCount)
intPage = intPageCount;
if(intPageCount>0)
rs.absolute((intPage-1)*intPageSize+1); //将记录指针定位到待显示页的第一条记录上
i = 0;
try{
//以下部分为循环显示从数据库中读取出来的数据
while(i<intPageSize && !rs.isAfterLast()){
num++;
String Shop_Id = rs.getString("Shop_Id");
String Shop_Name = rs.getString("Shop_Name");//店名
String Shop_Icon = rs.getString("Shop_Icon");//店标
String Shop_MainPro = rs.getString("Shop_MainPro");//主营产品
String Shop_Visitors = rs.getString("Shop_Visitors");//访问人数
String Shop_OpenTime = rs.getString("Shop_OpenTime");//所在地
String time = Shop_OpenTime.substring(0,10);
System.out.println("ShopList.jspxxxxxxxxxShop_Id="+Shop_Id);
%>
<%
if(num%2==0){
tdBgColor="#FFFFF7";
}
else{
tdBgColor="#EEFAFF";
}
%>
<tr>
<td width="119" bgcolor="<%=tdBgColor %>"><a href="main.jsp?ShopId=<%=Shop_Id%>" target="_blank"><img src="<%=Shop_Icon %>" width="119" height="114" border="0" /></a></td>
<td width="251" bgcolor="<%=tdBgColor %>"><a href="main.jsp?ShopId=<%=Shop_Id%>" target="_blank"><%=Shop_Name %></a></td>
<td width="83" bgcolor="<%=tdBgColor %>"><%=Shop_MainPro %></td>
<td width="63" bgcolor="<%=tdBgColor %>"><%=Shop_Visitors %></td>
<td width="63" bgcolor="<%=tdBgColor %>"><%=time %></td>
</tr> <%
rs.next();
}}catch(Exception e){
e.printStackTrace();
}finally{
rs.close();
db.closeDb();
}
%>
</table><form name="pageForm" action="ShopList.jsp" method="post">
<table align="right" width="745">
<tr>
<td>
共<%= intRowCount %>条记录,第<%=intPage %>/<%=intPageCount %>页 <%if(intPage<2){ %> 首页 | 上一页
<% }else{%> <a href="ShopList.jsp?page=1">首页</a>|<a href="ShopList.jsp?page=<%=intPage-1%>&">上一页</a>
<%}%>
<%if((intPageCount-intPage)<1){ %> 下一页 | 尾页
<%}else{%>
<a href="ShopList.jsp?page=<%=intPage+1%>"> 下一页</a> | <a href="ShopList.jsp?page=<%=intPageCount%>">尾页</a>
<%}%>
转到第<input type="text" name="page" size="3"/>页<input type="submit" value="Go" class="input1"/>
</td>
</tr>
</table>
</form>
</body>
</html>
数据库中有4条记录,每页显示15条,但是实际上只显示了第一条和最后一条记录出来;这是一种情况,还有一种情况就是设置每页显示1条记录,但是实际上却把所有的记录都罗列在首页。
不知道是什么原因,请高手指教!
while(i<intPageSize && !rs.isAfterLast())这个while 会把rs里面的记录都显示出来 你是一开始就把所有表里面的数据都拿出来了。
然后 在页面直接对数据库操作时不要的 分页最主要的一部是取数据 mysql 可以这样取你想要第几页的数据
select *from Table limit (pagenum-1)*rowsize+1,rowsize;
pagenum 是你想要第几页的数据 rowsize 一页显示的数据
http://download.csdn.net/source/1823849
select *from Table limit (pagenum-1)*rowsize+1,rowsize;
2楼说的这方法很好,可是我用的是sql server 2000 不支持limit 这种方式
I can't understand how to calculate the page count.
可以参考一下我写的分页:http://blog.csdn.net/Jerry_BJ/archive/2010/06/02/5642447.aspx
我在rs.next();后面加上一句:i++; (之前因为循环变量没有改变,所以不行)
这样改之后,读取其他的的数据表的内容可以正java.lang.NullPointerException
at org.apache.jsp.ShopList_jsp._jspService(ShopList_jsp.java:134)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
select top a * from table where id not in (select top b id from topic order by id) order by id
a表示每一页显示多少条数据numperpage, b表示前N条记录 numperpage * (currentPage - 1)
String Shop_OpenTime = rs.getString("Shop_OpenTime");
String time = Shop_OpenTime.substring(0,10);
我的数据表里Shop_OpenTime这个字段里的值为空,而我想截取空值的前11个字符,所以出现错误了。
只要先判断Shop_OpenTime是否为空,在截取字符,分页就正常显示了。
if(Shop_OpenTime!=null)
{
String Shop_OpenTime = rs.getString("Shop_OpenTime");
String time = Shop_OpenTime.substring(0,10);
}