翻页的做法的一种:把查询结果的主键隐藏在页面上:
<input type="hidden" name="PrimaryKey" value="<%=strKey%>">
并且设定一个当前页面使用的主键
<input type="hidden" name="CurrentKey" value="<%=strCurrentKey%>">
然后根据这些信息就可以在用户点击"翻页"的时候,进行处理。我曾经写过的一个程序,可能不是太好,便比较简单易懂:
示例:
int intPageSize; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数
int intPage; //待显示页码
int i,j,k; //设置一页显示的记录数 public String backdata(int count,String strPage){
PreparedStatement stmt=null;
ResultSet rs=null;
intPageSize = 6; //每页显示6条纪录
intRowCount=count;
String backStr="";
if(strPage==null){
intPage = 1;
}else{
intPage = Integer.parseInt(strPage);
if(intPage<1)
intPage = 1;
}
//计算总页数
intPageCount =(intRowCount+intPageSize-1) / intPageSize;
//调整待显示的页码
if(intPage>intPageCount) intPage = intPageCount;
String secondTr="<tr><td nowrap>选择</td><td nowrap>标题</td><td nowrap>文件名</td><td nowrap nowrap>录入部门</td><td nowrap>填写人</td><td nowrap>发布时间</td></tr>";
String printStr="";
try{
stmt=((Connection)this.getEnvironment().lookup("env:res/connection/awh")).prepareStatement(
"select * from law_statute order by law_publt desc");
rs=stmt.executeQuery();
i = (intPage-1) * intPageSize;
for(j=0;j<i;j++)
rs.next();
//显示数据
i = 0;
while(i<intPageSize && rs.next()){
String cid=rs.getString("cid");
String lawtitle=rs.getString("law_title");
Date date=rs.getDate("law_publt");
String lawfilen=rs.getString("law_filen");
String writdept=rs.getString("writ_dept");
String writperson=rs.getString("writ_person");
String lawpublt=date.toString();
String checkNa="chekb";//+String.valueOf(i);
printStr=printStr+"<tr><td><input type='checkbox' name='"+checkNa+"' id='"+cid+"'></td><td>"+lawtitle+"</td><td>"+lawfilen+"</td><td>"+writdept+"</td><td>"+writperson+"</td><td>"+lawpublt+"</td></tr>";
i++;
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
}catch(Exception e){}
}
String dataStr="<%strPage = request.getParameter('page');%>";
String pageStr="第"+intPage+"页 "+" 共"+intPageCount+"页";
String forPgStr="";
String backPgStr="";
if(intPage<intPageCount){
int pageNum=intPage+1;
forPgStr="<a href='lawstatute.jsp?page="+pageNum+"'>下一页</a>";
}
if(intPage>1){
int pageNum=intPage-1;
backPgStr="<a href='lawstatute.jsp?page="+pageNum+"'>上一页</a>";
}
String firstTr="<table name='table1' border='1'><tr><td colspan='2'>"+pageStr+"</td><td colspan='2'><b>法律法规</b></td><td colspan='2'>"+forPgStr+" "+backPgStr+"</td></tr>";
String lastTr="<tr><td colspan='6'><input type='checkbox' name='checkAll' onclick='checkedAll()'>全选 <label onclick='addData()' style='cursor:hand'>增加</label> <label onclick='amendData()' style='cursor:hand'>修改</label> <label onclick='deleteData()' style='cursor:hand'>删除</label></td></tr>";
backStr=dataStr+firstTr+secondTr+printStr+lastTr+"</table>";
return backStr;
}
/*--------------------------- end ----------------------------------------*/
<input type="hidden" name="PrimaryKey" value="<%=strKey%>">
并且设定一个当前页面使用的主键
<input type="hidden" name="CurrentKey" value="<%=strCurrentKey%>">
然后根据这些信息就可以在用户点击"翻页"的时候,进行处理。我曾经写过的一个程序,可能不是太好,便比较简单易懂:
示例:
int intPageSize; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数
int intPage; //待显示页码
int i,j,k; //设置一页显示的记录数 public String backdata(int count,String strPage){
PreparedStatement stmt=null;
ResultSet rs=null;
intPageSize = 6; //每页显示6条纪录
intRowCount=count;
String backStr="";
if(strPage==null){
intPage = 1;
}else{
intPage = Integer.parseInt(strPage);
if(intPage<1)
intPage = 1;
}
//计算总页数
intPageCount =(intRowCount+intPageSize-1) / intPageSize;
//调整待显示的页码
if(intPage>intPageCount) intPage = intPageCount;
String secondTr="<tr><td nowrap>选择</td><td nowrap>标题</td><td nowrap>文件名</td><td nowrap nowrap>录入部门</td><td nowrap>填写人</td><td nowrap>发布时间</td></tr>";
String printStr="";
try{
stmt=((Connection)this.getEnvironment().lookup("env:res/connection/awh")).prepareStatement(
"select * from law_statute order by law_publt desc");
rs=stmt.executeQuery();
i = (intPage-1) * intPageSize;
for(j=0;j<i;j++)
rs.next();
//显示数据
i = 0;
while(i<intPageSize && rs.next()){
String cid=rs.getString("cid");
String lawtitle=rs.getString("law_title");
Date date=rs.getDate("law_publt");
String lawfilen=rs.getString("law_filen");
String writdept=rs.getString("writ_dept");
String writperson=rs.getString("writ_person");
String lawpublt=date.toString();
String checkNa="chekb";//+String.valueOf(i);
printStr=printStr+"<tr><td><input type='checkbox' name='"+checkNa+"' id='"+cid+"'></td><td>"+lawtitle+"</td><td>"+lawfilen+"</td><td>"+writdept+"</td><td>"+writperson+"</td><td>"+lawpublt+"</td></tr>";
i++;
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
}catch(Exception e){}
}
String dataStr="<%strPage = request.getParameter('page');%>";
String pageStr="第"+intPage+"页 "+" 共"+intPageCount+"页";
String forPgStr="";
String backPgStr="";
if(intPage<intPageCount){
int pageNum=intPage+1;
forPgStr="<a href='lawstatute.jsp?page="+pageNum+"'>下一页</a>";
}
if(intPage>1){
int pageNum=intPage-1;
backPgStr="<a href='lawstatute.jsp?page="+pageNum+"'>上一页</a>";
}
String firstTr="<table name='table1' border='1'><tr><td colspan='2'>"+pageStr+"</td><td colspan='2'><b>法律法规</b></td><td colspan='2'>"+forPgStr+" "+backPgStr+"</td></tr>";
String lastTr="<tr><td colspan='6'><input type='checkbox' name='checkAll' onclick='checkedAll()'>全选 <label onclick='addData()' style='cursor:hand'>增加</label> <label onclick='amendData()' style='cursor:hand'>修改</label> <label onclick='deleteData()' style='cursor:hand'>删除</label></td></tr>";
backStr=dataStr+firstTr+secondTr+printStr+lastTr+"</table>";
return backStr;
}
/*--------------------------- end ----------------------------------------*/
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货