读书人

奇怪:jsp 分页有关问题

发布时间: 2012-01-02 22:40:04 作者: rapoo

奇怪:jsp 分页问题
问题在于,首页显示正确,但是后面的下一页等均不对???
javabean代码:
package spage;
import java.sql.*;

public class ShowPage {
public String fluxPage(ResultSet rs,int p,int size){
String str= " ";
try{
for(int k=0;k <(p-1)*size;k++){
rs.next();
}
}
catch(SQLException e){
e.printStackTrace();
}
str= " <tr> <th> "+ "业务类别 "+ " </th> <th> "+ "用户ID "+ " </th> <th> "+ "居民区ID "+ " </th> <th> "+ "用水量 "+ " </th> <th> "+ "日期 "+ " </th> </tr> ";
for(int iPage=0;iPage <size;iPage++){
str+=fluxRow(rs,iPage,size);
try{
if(!rs.next())break;
}
catch(Exception e){
e.printStackTrace();
}
}
return str;
}
public static String fluxRow(ResultSet rs,int i,int size){
String temp= " <tr> <td> 个人用户 </td> ";

try{
temp+= " <td> "+Integer.parseInt(rs.getString( "userid "))+ " </td> ";
temp+= " <td> "+Integer.parseInt(rs.getString( "resideid "))+ " </td> ";
temp+= " <td> "+Integer.parseInt(rs.getString( "flux "))+ " </td> ";
temp+= " <td> "+rs.getString( "date ")+ " </td> </tr> ";
}
catch(Exception e){
e.printStackTrace();
}
return temp;
}
}
jsp代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">

<%@ page language= "java " contentType= "text/html; charset=gb2312 "%>
<%@ page import= "java.sql.* " %>
<%@ page import= "java.io.* " %>
<jsp:include page= "top.jsp " flush= "true " />

<jsp:useBean id= "conn " class= "dbBean.DbBean " scope= "session "/>
<jsp:useBean id= "sp " class= "spage.ShowPage " scope= "session "/>
<jsp:setProperty name= "conn " property= "url " value= "jdbc:mysql://localhost/test "/>

<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=ISO-8859-1 ">
<title> 用户用水信息 </title>
</head>
<body>
<center>
<div id= "wrapper ">
<form action= "fluxlist.jsp " method= "get "width= "80% ">
<%
String userid=null;
String startdate=null;
String enddate=null;
int totalFlux=0;
String sql=null;
ResultSet rs=null;



userid=(String)session.getAttribute( "userid ");


startdate=request.getParameter( "starttime1 ")+ "- "+request.getParameter( "starttime2 ")+ "- "+request.getParameter( "starttime3 ");
enddate=request.getParameter( "endtime1 ")+ "- "+request.getParameter( "endtime2 ")+ "- "+request.getParameter( "endtime3 ");

sql= "SELECT sum(flux) FROM `waterflux` WHERE date BETWEEN ' "+startdate+ " ' AND ' "+enddate+ " ' AND userid= ' "+userid+ " ' ";

rs=conn.executeQuery(sql);
try
{
if(rs.next())
{
if(rs.getString( "sum(flux) ")!=null)
{
totalFlux=Integer.parseInt(rs.getString( "sum(flux) "));
}

}
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(NumberFormatException e)
{
e.printStackTrace();
PrintWriter wr=response.getWriter();
wr.print( "数据格式错误! ");
}

%>
<table>
<tr> <td>
共计用水量为 <%=totalFlux%> 吨
</td> </tr>
</table>
<table border= "2 ">
<%
int size=3;
int totalRecord=0;
int totalPage=0;
int Page=1;
String str= " ";
sql= "SELECT * FROM `waterflux` WHERE date BETWEEN ' "+startdate+ " ' AND ' "+enddate+ " ' AND userid= ' "+userid+ " ' ";
rs=conn.executeQuery(sql);
try{
if(rs.next())
{
rs.last();
totalRecord=rs.getRow();
}
rs.first();
}

catch(SQLException e){
e.printStackTrace();
PrintWriter wr=response.getWriter();
wr.print( "数据库操作失败! ");

}
try{
if(totalRecord%size==0)
{
totalPage=totalRecord/size;
}
else
{
totalPage=totalRecord/size+1;
}

if(totalPage==0)
{
totalPage=1;
}

}
catch(NullPointerException e)
{
e.printStackTrace();
PrintWriter wr=response.getWriter();
wr.print( "空指针! ");

}
catch(NumberFormatException e)
{
e.printStackTrace();
PrintWriter wr=response.getWriter();
wr.print( "数据格式错误! ");
}

if((request.getParameter( "page ")==null)||(request.getParameter( "page ").equals( " ")))
{
if(session.getAttribute( "userp ")==null)
{
Page=1;
}
else
{
Page=((Integer)session.getAttribute( "userp ")).intValue();
}
}
else
{
Page=Integer.parseInt(request.getParameter( "page "));
session.setAttribute( "userp ",new Integer(Page));


}


try
{
str=sp.fluxPage(rs,Page,size);
// session.setAttribute( "data ",str);
out.print(str);
}
catch(NullPointerException e)
{
e.printStackTrace();
PrintWriter wr=response.getWriter();
wr.print( "空指针! ");
}
%>
</table>

<table>
<tr>
<td> 共有查询结果 <%=totalRecord%> 条 </td> <td> 当前 <%=Page%> / <%=totalPage%> 页 </td>
<%if(Page!=1){ %>
<td> <a href= "fluxlist.jsp?page=1 "> 首页 </a> </td>
<%}else{%>
<td> 首页 </td>
<%}if(Page> 1){ %>
<td> <a href= "fluxlist.jsp?page= <%=Page-1%> "> 上一页 </a> </td>
<%}else{%>
<td> 上一页 </td>
<%}
int pagenum=2;
int temp=Page/2;
int temp1=Page%2;
if(temp==0)
{
for(int i=1;(i <=pagenum)&&(i <=totalPage);i++)
out.print( " <td> <a href=fluxlist.jsp?page= "+i+ "> "+i+ " </a> </td> ");
}
else if(temp1==0)
{
for(int i=(temp-1)*pagenum+1;(i <=temp*pagenum)&&(i <totalPage);i++)
out.print( " <td> <a href=fluxlist.jsp?page= "+i+ "> "+i+ " </a> </td> ");
}

if(Page <totalPage){
%>
<td> <a href= "fluxlist.jsp?page= <%=Page+1%> "> 下一页 </a> </td>
<%}else{%>
<td> 下一页 </td>
<%}if(Page!=totalPage){%>
<td> <a href= "fluxlist.jsp?page= <%=totalPage %> "> 尾页 </a> </td>
<%}else{%>
<td> 尾页 </td>
<%} %>
<td> 请输入页码数: </td> <td> <input type= "text " name= "page "value= <%=Page%> size= "2 "> </td>
<td> <input type= "submit " name= "submit " value= "goto ">
</tr>
</table>
</form>
</div>
</center>
<%
rs.close();
conn.close();
%>
</body>
</html>

[解决办法]
url上有参数page;
startdate=request.getParameter( "starttime1 ")+ "- "+request.getParameter( "starttime2 ")+ "- "+request.getParameter( "starttime3 ");
enddate=request.getParameter( "endtime1 ")+ "- "+request.getParameter( "endtime2 ")+ "- "+request.getParameter( "endtime3 ");
那么这里的参数怎么得到呢???starttime1等,哪得来呢???
[解决办法]
在javabean里面写个分页方法:
public boolean startPagiation() throws Exception {
if (rs == null) {
throw new Exception( "ResultSet is NULL! ");
}
rs.last();
int setCnt = rs.getRow();
rs.beforeFirst();
pageSize = setCnt / pageRowCount;
if (setCnt % pageRowCount > 0) {
pageSize++;
}
return true;
}


转页:
public void gotoPage(int pageNo) throws Exception {
if (rs == null) {
throw new Exception( "ResultSet is NULL! ");


}
if (pageNo < 1) {
pageNo = 1;
} else if (pageNo > pageSize) {
pageNo = pageSize;
}
curPage = pageNo;
curPos = 1;
if (curPage == 1) {
rs.beforeFirst();
} else {
rs.absolute((pageNo - 1) * pageRowCount);
}
}


还有查询结果集:
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
Record = stmt.executeQuery(sSql);

读书人网 >Java Web开发

热点推荐