CREATE PROCEDURE SP_GetauthorPage( @CurrentPage int, @PageSize int ) AS declare @sSQL1 varchar(500) declare @sSQL2 varchar(500) declare @iCount int declare @PageCount int declare @RecordCount intset @iCount=@CurrentPage*@PageSize print @iCountselect @RecordCount=Count(*) from author print @RecordCountif @RecordCount >0 begin if @RecordCount % @PageSize=0 begin set @PageCount=@RecordCount/@PageSize end else begin set @PageCount=@RecordCount/@PageSize+1 end if @CurrentPage<=@PageCount and @CurrentPage>0 begin set @sSQL1='(select top '+ ltrim(rtrim(str(@iCount))) +' * from author order by au_lname ) as tmp' --print @sSQL1
set @sSQL2='select top ' +ltrim(rtrim(str(@PageSize)))+' * from '+@sSQL1 +' order by au_lname desc ' print @sSQL2
EXEC(@sSQL2) end else begin select top 0 * from author end
我最早也是采用你那种方法,即将所有的纪录全部取出来,存在Vector中,我是把vector放在了一个bean中,但那会我们数据库中最多才3000多条记录。现在已经有10几万的记录了,所以将纪录全部取出,就造成了out of memory! 你觉得我后来的方案如何呢?(14:57分发的那个)这点我也想过,所以我还会再做一个功能,就是在以得到结果中的再次查询(怎么实现还没想好)
@CurrentPage int,
@PageSize int
) AS
declare @sSQL1 varchar(500)
declare @sSQL2 varchar(500)
declare @iCount int
declare @PageCount int
declare @RecordCount intset @iCount=@CurrentPage*@PageSize
print @iCountselect @RecordCount=Count(*) from author
print @RecordCountif @RecordCount >0
begin
if @RecordCount % @PageSize=0
begin
set @PageCount=@RecordCount/@PageSize
end
else
begin
set @PageCount=@RecordCount/@PageSize+1
end
if @CurrentPage<=@PageCount and @CurrentPage>0
begin
set @sSQL1='(select top '+ ltrim(rtrim(str(@iCount))) +' * from author order by au_lname ) as tmp'
--print @sSQL1
set @sSQL2='select top ' +ltrim(rtrim(str(@PageSize)))+' * from '+@sSQL1 +' order by au_lname desc '
print @sSQL2
EXEC(@sSQL2)
end
else
begin
select top 0 * from author
end
end
else
begin
select * from author
end
GO
(美)Bill Brogden/ 7-5053-6658-0/电子工业出版社
to hegum(hg):
你能告诉我那本书里例子的思路吗?
你觉得我后来的方案如何呢?(14:57分发的那个)这点我也想过,所以我还会再做一个功能,就是在以得到结果中的再次查询(怎么实现还没想好)
采用vector解决对于数据量小比较合适,大数据量不行,一次查询放到session里,如果用户量很大,服务器可能撑不住
然后再做一个JavaBean用来访问上面一个JavaBean,提供一些方法来实现分页查询。
做一个类似监听器的程序,如果数据库有更新,就自动的更新第一个JavaBean.缺点,如果类似的分页页面很多就特别耗内存,但只有一两个应该行吧?
每次取十页放在Session中,十页十页的从数据库中取。
页面上可以提供 前十页 1 2 3 4 5 6 7 8 9 10 后十页缺点速度稍慢,
Java开发指南——Servlets和JSP篇
(美)Bill Brogden/ 7-5053-6658-0/电子工业出版社
里面有连接池等提高性能的东西!可惜我没有这本书,以前在学校图书馆看的
还有,在分页显示的时候最好判断一下,得到的页面号码是不是为“null”,如果不为NULL就不要去查询数据库。