JAVA分页总结
JAVA分页总结
环境:数据库用的是MYSQL
不走1:前端页面增加“上一页,下一页”……
<ahref="?startindex=<%=preindex%>&pagesize=10">上一页</a>
<ahref="?startindex=<%=nextindex%>&pagesize=10">下一页</a>
不走2:JSP页面增加代码
recordop c= new recordop();
c.request=request;
ResultSet rs=c.list("uname"); //取得查询纪录
Stringpreindex=c.getpreindex();
String nextindex=c.getnextindex();
不走3:后端JAVA增加以下3个方法用来计算上一页及下一页索引
publicintpagesize=12;
public String getpreindex()
{
int curindex=0;
if(request.getParameter("startindex")==null)
return"0";
else
{
curindex=Integer.valueOf(request.getParameter("startindex"));
int pre=curindex-pagesize;
if(pre<0)
return"0";
else
return String.valueOf(pre);
}
}
public String getCurIndex()
{
int curindex=0;
if(request.getParameter("startindex")==null)
return"0";
else
{
return (request.getParameter("startindex"));
}
}
public String getnextindex()
{
int curindex=0;
if(request.getParameter("startindex")==null)
{
int next=curindex+pagesize;
return String.valueOf(next);
}
else
{
curindex=Integer.valueOf(request.getParameter("startindex"));
int next=curindex+pagesize;
return String.valueOf(next);
}
}
不走4:增加LIST方法,查询纪录:主要语句为
String sql = "select *from vinrecord order by id desc limit "+getCurIndex()+","+String.valueOf(pagesize);