哭啊,刚才点错了,本来就写好了,强烈建议增加自动保存功能!!!!!!!!!!!!!!
封装类的初始化程序如:
public
synchronized
void
init(String sql1, String sql2,
int
ps, String cp)
...
{
setCountSqlStr(sql1);
setQuerySqlStr(sql2);
conn = DatabaseBean.getConnection();
this.pagesize = getPageSize(ps); // 设置每页显示条数
this.totalcounts = getTotalCounts(); // 设置总条数
this.totalpages = getTotalPages(); // 取得总页数
this.currentpage = getCurrentPage(cp); // 取得当前页
}
setCountSqlStr(sql1);
setQuerySqlStr(sql2);
conn = DatabaseBean.getConnection();
this.pagesize = getPageSize(ps); // 设置每页显示条数
this.totalcounts = getTotalCounts(); // 设置总条数
this.totalpages = getTotalPages(); // 取得总页数
this.currentpage = getCurrentPage(cp); // 取得当前页
}
sql可能是这样:
sql1
=
"
select count(*) from table
"
;
sql2 = " select * from table " ;
sql2 = " select * from table " ;
在jsp页面代码如下:
rowNum
=
PageList.getCurrentPageSize();
int firstRowNum = PageList.getFirstRowNum();
try
... {
for (int i = 0; i < rowNum; i++)
...{
int num = firstRowNum+i+1;
rs.next();
rs.getString(1);
rs.getString(2);
rs.getString(3);
rs.getString(4);
}
} catch (Exception e) ... {
} finally ... {
PageList.destory();
}
int firstRowNum = PageList.getFirstRowNum();
try
... {
for (int i = 0; i < rowNum; i++)
...{
int num = firstRowNum+i+1;
rs.next();
rs.getString(1);
rs.getString(2);
rs.getString(3);
rs.getString(4);
}
} catch (Exception e) ... {
} finally ... {
PageList.destory();
}
这样的做法是这样的:将所有记录的信息都保存在ResultSet中,然后在页面上通过循环控制某条记录是否显示在页面上。这样的弊端就是将所有记录的信息都查询到内存中,如果记录集很大的话,势必影响性能。
最近看了别人bbs系统中对于分页的实现,也是jsp版本的,原理如下:
int pageSize = 50;
int currentPage;
int startNum = (pageSize-1)*currentPage+1;
int endNum = pageSize*currentPage;
int currentPage;
int startNum = (pageSize-1)*currentPage+1;
int endNum = pageSize*currentPage;
public String getList()
...{
Connection conn = null;
ResultSet rs = null;
PreparedStatement stmt = null;
...{
Connection conn = null;
ResultSet rs = null;
PreparedStatement stmt = null;
conn = DatabaseBean.getConnection();
String s = "";
StringBuffer sb = new StringBuffer("");
try...{
String strSQL = "select id from (select id,rownum rn from logManage order by id asc) where rowNum between ? and ? ";
stmt = conn.prepareStatement(strSQL);
stmt.setInt(1,startNum);
stmt.setInt(2,endNum);
rs = stmt.executeQuery();
while(rs != null && rs.next())
...{
sb.append(id);
sb.append(",");
}
rs.close();
rs = null;
}catch(Exception e)...{
}finally...{
try...{
if(conn != null)
conn.close();
}catch(Exception ex)...{}
}
String s = "";
StringBuffer sb = new StringBuffer("");
try...{
String strSQL = "select id from (select id,rownum rn from logManage order by id asc) where rowNum between ? and ? ";
stmt = conn.prepareStatement(strSQL);
stmt.setInt(1,startNum);
stmt.setInt(2,endNum);
rs = stmt.executeQuery();
while(rs != null && rs.next())
...{
sb.append(id);
sb.append(",");
}
rs.close();
rs = null;
}catch(Exception e)...{
}finally...{
try...{
if(conn != null)
conn.close();
}catch(Exception ex)...{}
}
s = sb.substring(0,sb.length()-1);
return s;
}
return s;
}
这样做的道理是,将当前也要显示的记录的id号先查询出来,然后在jsp页面中的sql可以这样写:
String strSQL
=
"
select * from table where id in (上面函数得到的s) order by id asc
"
;
这样保证了查询的结果集都是当前页面的一个结果集,因为每页所显示的记录数都很有限,所以即使数据量很大也是没有关系。
因为对于缓存的处理还没有什么概念,所以不在本文的思考范围内。
这只是个人在学习过程中的一些个人思考,有错的地方请指出,联系我:aimer311@sina.com