select top (pagepageSize) from user where userID not in (select top (page-1)pageSize) userID from user)
望采纳import javasqlConnection;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javautilArrayList;
public class goodsBeanC {
//定义数据库变了
private ResultSet rs=null;
private PreparedStatement ps=null;
private Connection con=null;
private int pageSize = 6; //定义分页大小
private int rowCount = 0; //记录总数
private int pageCount = 0; //得到分页的页数
/
@param pageSize 每个分页的页面大小
@param rowCount 数据库中记录的总数
@param pageCount 得到可以分页的页数
@return pageCount 分页的页数
/
public int getPageCount(){
//得到数据库连接
con=new ConDB()getCon();
try {
//获取数据库中符合条件的记录的总数
ps=conprepareStatement("select count() from goods");
rs=psexecuteQuery();
while(rsnext()){
rowCount=rsgetInt(1);
}
//计算可以分的页数
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=(rowCount/pageSize)+1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}finally{
thisclose();
}
return pageCount;
}
/
@param goodsId 货物编号
@return gb(goodsBean) 得到封装的货物信息
/
public goodsBean getGoodsBean(String goodsId){
goodsBean gb=new goodsBean();
try {
con=new ConDB()getCon();
ps=conprepareStatement("select from goods where goodsId=");
pssetString(1, goodsId);
rs=psexecuteQuery();
while(rsnext()){
gbsetGoodsId(IntegerparseInt(rsgetString(1)));
gbsetGoodsName(rsgetString(2));
gbsetGoodsIntro(rsgetString(3));
gbsetGoodsPrice(rsgetFloat(4));
gbsetGoodsNum(rsgetInt(5));
gbsetPublish(rsgetString(6));
gbsetImages(rsgetString(7));
gbsetType(rsgetString(8));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}finally{
thisclose();
}
return gb;
}
/
@param pageNow 当前页数
@return ArrayList 返回的数据库中的记录存放在al中
/
public ArrayList getPageNow(int pageNow){
ArrayList al=new ArrayList();
con=new ConDB()getCon();
try {
ps=conprepareStatement
("select top " + pageSize
+ " from goods where goodsId not in(select top "
+ pageSize (pageNow - 1) + " goodsId from goods)"); //分页的sql语句
rs=psexecuteQuery();
while(rsnext()){
//将每条结果的每个属性封装到gb中
goodsBean gb=new goodsBean();
gbsetGoodsId(IntegerparseInt(rsgetString(1)));
gbsetGoodsName(rsgetString(2));
gbsetGoodsIntro(rsgetString(3));
gbsetGoodsPrice(rsgetFloat(4));
gbsetGoodsNum(rsgetInt(5));
gbsetPublish(rsgetString(6));
gbsetImages(rsgetString(7));
gbsetType(rsgetString(8));
//将gb封装到ArrayList中
aladd(gb);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}finally{
thisclose();
}
return al;
}
/
关闭数据库连接
/
public void close() {
try {
if (rs != null)
rsclose();
if (ps != null)
psclose();
if (con != null)
conclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}intPageCount = (intRowCount+intPageSize-1) / intPageSize;等价于
intPageCount=(intRowCount-1/intPageSiez)+1;
如果introwCount(总记录)=8,intPageSize(每页记录)=8则共为1页,如果rowCount=14,inPageSize=8则为2页,就是说总页数至少是要比8的倍数多1才会多翻一页你想想如果一页显示八个记录,而总数刚好最后页有8个记录,你会翻页吗,不会的吧!!就这个道理
其实可以用为intPageCount=intRowCount%intPageSize==0(intRowCount)/intPageSize:(intRowCount/intPageSize+1);
假设rowCount(总记录数)=8 intPageSize(每页记录数)=8,那么总页数就是因为8%8==0则8/8=1
就是一页
假设rowCount=14 intPageSize=8则总页数就是因为14%8!=0则14/8+1=2
就是两页这样分析楼主不知道能否明白分页最重要的是原理:
就是读取你所想要的数据,那最重要的就是sql语句的写法,如果是使用sqlserver数据库
分页的写法 select top 10 from 表 where id not in(select top10 id from 表)这里假定每页显示10条数据,这里读取的是第二页的数据,如果读取第三页的数据,子查询里面就变成 top 20 ,总结为(页数-1)当前页面的条数,希望能对有帮助。<body>
当前搜索条件:
<c:if test="${cond==null}">空</c:if>
<c:if test="${cond!=null}">${cond}</c:if>
<form action="page" method="post">
<input type="text" name="name" />
<input type="submit" value="按名字模糊搜索" />
</form>
<table>
<tr>
<td>商品编号</td>
<td>类型编号</td>
<td>商品名称</td>
<td>商品说明</td>
<td>商品</td>
<td>商品日期</td>
<td>详细</td>
</tr>
<!-- 循环遍历ps对象中的List<Address>集合 -->
<c:forEach items="${spsinfos}" var="ad">
<tr>
<td>${adshopId}</td>
<td>${adtypeId}</td>
<td>${adshopName}</td>
<td>${adshopSay}</td>
<td>${adshopPrice}</td>
<td>${addefaultPic}</td>
<td>${adupMetailDate}</td>
</tr>
</c:forEach>
</table>
<a href="pagecp=1">首页</a>
<a href="pagecp=${(spscurrentPage-1)<=11:(spscurrentPage-1)}">上页</a>
<a href="pagecp=${(spscurrentPage+1)>=(spscountPage)(spscountPage):(spscurrentPage+1)}">下页</a>
<a href="pagecp=${spscountPage}">末页</a>
</body>
这是我之前写的DEMO,对应的把属性改成你写好的ACTION就可以了伪代码提供思路
int page=1;//要显示第几页,页面将此参数传过来
int size = 10;//一页多少条
int rows = 0;//总共多少条
ResultSet rs = statementexecuteQuery("select count() as nums from XX表 where XX条件");
while(rsnext()) {rows = rsgetInt("nums");}
int pages = (rows%size == 0)(rows/size):(rows/size + 1);//得到总页数
string sql="";
int fnum ;//起始游标
int enum;//结束游标
if (page > pages){
//这还查个毛啊
}else if (page == pages){
fnum = (page-1)size;
sql = "select from XX表 where XX条件 limit "+fnum+","+rows;
}else if (page < pages){
fnum = (page-1)size;
enum = pagesize-1;
sql = "select from XX表 where XX条件 limit "+fnum+","+enum;
}
//接下来不用说了吧,
PS:伪代码,仅提供思路
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)