求助:分页的时候出现的错误
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@page import="cn.yz.factory.DAOFactory,cn.yz.dao.*,cn.yz.vo.*" %>
<%@page import="java.util.*" %>
<style type="text/css">
.STYLE6 {font-size: 12px}
.STYLE10 {font-size: 14px; font-weight: bold; }
table
{
border-collapse:collapse;
width:100%;
}
table,th,td
{
border:1px solid blue;
}
</style>
<script type="text/javascript">
function changeColor(obj,color){
obj.bgColor = color ;
}
</script>
<%!
public static final String URL = "product_list.jsp" ;
%>
<%
int currentPage = 1 ;// 为当前所在的页,默认在第1页
int lineSize = 3 ;// 每次显示的记录数
long allRecorders = 0 ;// 表示全部的记录数
String keyWord = request.getParameter("kw") ;// 接收查询关键字
%>
<%
try{
currentPage = Integer.parseInt(request.getParameter("cp")) ;
} catch(Exception e) {}
try{
lineSize = Integer.parseInt(request.getParameter("ls")) ;
} catch(Exception e) {}
if(keyWord == null){
keyWord = "" ;// 如果模糊查询没有关键字,则表示查询全部
}
%>
<%
IProductDAO dao = DAOFactory.getIProductDAOInstance() ;
List<Product> all = dao.findAll(keyWord,currentPage,lineSize) ;
allRecorders = dao.getAllCount(keyWord);
%>
<div align="center">
<h1>商品列表</h1><%=allRecorders%>
<jsp:include page="split_page_plugin.jsp">
<jsp:param name="allRecorders" value="<%=allRecorders%>"/>
<jsp:param name="url" value="<%=URL%>"/>
<jsp:param name="currentPage" value="<%=currentPage%>"/>
<jsp:param name="lineSize" value="<%=lineSize%>"/>
</jsp:include>
<table>
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE10">编号</span></td>
<td align="center" valign="middle"><span class="STYLE10">名称</span></td>
<td align="center" valign="middle"><span class="STYLE10">价格</span></td>
<td align="center" valign="middle"><span class="STYLE10">数量</span></td>
<td align="center" valign="middle"><span class="STYLE10">简介</span></td>
<td align="center" valign="middle"><span class="STYLE10">购买</span></td>
</TR>
<%
Iterator<Product> iter = all.iterator() ;
while(iter.hasNext()){
Product pro = iter.next() ;
%>
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE6"><%=pro.getPid()%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><a href="product_show.jsp?pid=<%=pro.getPid()%>"><%=pro.getName()%></a></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=pro.getPrice()%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=pro.getAmount()%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=pro.getNote()%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=pro.getCount()%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><a href="add_car.jsp?pid=<%=pro.getPid()%>" target="_ablank">增加到购物车</a></span></td>
</TR>
<%
}
%>
</table>
</div>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%
int currentPage = 1 ;// 为当前所在的页,默认在第1页
int lineSize = 3 ;// 每次显示的记录数
long allRecorders = 0 ;// 表示全部的记录数
long pageSize = 1 ;// 表示全部的页数(尾页)
int lsData[] = {1,3,5,7,9,10,15,20,25,30,50,100} ;
String keyWord = request.getParameter("kw") ;// 接收查询关键字
String url = request.getParameter("url") ;
%>
<%
try{
currentPage = Integer.parseInt(request.getParameter("currentPage")) ;
} catch(Exception e) {}
try{
lineSize = Integer.parseInt(request.getParameter("lineSize")) ;
} catch(Exception e) {}
try{
allRecorders = Long.parseLong(request.getParameter("allRecorders")) ;
} catch(Exception e) {}
if(keyWord == null){
keyWord = "" ;// 如果模糊查询没有关键字,则表示查询全部
}
%>
<%
pageSize = (allRecorders + lineSize -1) / lineSize ;
if(pageSize == 0){
pageSize = 1 ;
}
%>
<script type="text/javascript">
function go(num){
document.getElementById("cp").value = num ;
document.spform.submit() ;// 表单提交
}
</script>
<form name="spform" action="<%=url%>" method="post">
输入查询关键字:<input type="text" name="kw" value="<%=keyWord%>">
<input type="submit" value="查询"><br>
<input type="button" value="首页" onclick="go(1)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="上一页" onclick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="下一页" onclick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>>
<input type="button" value="尾页" onclick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>>
跳转到第<select name="selcp" onchange="go(this.value)">
<%
for(int x=1;x<=pageSize;x++){
%>
<option value="<%=x%>" <%=x==currentPage?"SELECTED":""%>><%=x%></option>
<%
}
%>
</select>页
每页显示
<select name="ls" onchange="go(1)">
<%
for(int x=0;x<lsData.length;x++){
%>
<option value="<%=lsData[x]%>" <%=lsData[x]==lineSize?"SELECTED":""%>><%=lsData[x]%></option>
<%
}
%>
</select>
条
<input type="hidden" name="cp" value="1">
</form>
public long getAllCount(String keyWord) throws Exception {
long count = 0;
String sql = "select count(pid) from product where name LIKE ? OR note LIKE ? OR price LIKE ? OR amount LIKE ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%"+keyWord+"%");
pstmt.setString(2, "%"+keyWord+"%");
pstmt.setString(3, "%"+keyWord+"%");
pstmt.setString(4, "%"+keyWord+"%");
ResultSet rs = pstmt.executeQuery();
if (rs.next()) { // 取得全部的记录数
count = rs.getInt(1);
}
return count;
}