大家好,,现在说明下我的问题:我在做一个论坛,,其中在首页显示的是全部的贴子,包括主题贴和回复贴,
现在我想使用分页的方法,把贴子分页显示,,显示时,调用的是下面的方法<%!
private static void tree (List<Article> articles , Connection conn, int id ,int grade,int startPos,int endPos,int count){
//DB是我写的一个连接数据库的一个小CLASS, 这里没有问题.
String sql = "select * from article where pid = "+ id ;
Statement stmt = DB.getStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql) ;
try {
while ( rs.next() ){
count ++ ;
Article a = new Article() ;
if( count >= startPos && count <= endPos){
a.setId(rs.getInt("id"));
a.setPid(rs.getInt("pid"));
a.setRootId(rs.getInt("rootid"));
a.setTitle(rs.getString("title"));
a.setPDate(rs.getTimestamp("pDate"));
a.setGrade(grade + 1) ;
articles.add(a);
System.out.println("count: "+count+" 容器的大小:"+articles.size());
}
//
if ( !a.isLeaf() && count <= endPos){ //又是一个递归!!!!
tree(articles,conn,rs.getInt("id"),grade+1,startPos,endPos,count);
System.out.println("count: "+count+" 容器的大小:"+articles.size());
}
}
//System.out.println("count:"+count+"startPOs:"+startPos+"endPos:"+endPos);
}catch(SQLException e){
e.printStackTrace();
}
}
%>
在调用时,参数传递为:
tree(articles,conn,0,0,startPos,endPos,0);其中statrPos =0 endPos = 4 就是想一页显示5条记录,下面是我后台打印的结果:count: 1 容器的大小:1
count: 2 容器的大小:2
count: 3 容器的大小:3
count: 4 容器的大小:4
count: 4 容器的大小:4
count: 3 容器的大小:4
count: 4 容器的大小:5
count: 4 容器的大小:5
count: 2 容器的大小:5
count: 3 容器的大小:6
count: 4 容器的大小:7
count: 4 容器的大小:7
count: 3 容器的大小:7
count: 4 容器的大小:8
count: 4 容器的大小:8
count: 1 容器的大小:8
count: 2 容器的大小:9
count: 3 容器的大小:10
count: 3 容器的大小:10
count: 4 容器的大小:11
count: 4 容器的大小:11
count: 2 容器的大小:11
count: 3 容器的大小:12
count: 3 容器的大小:12
count: 4 容器的大小:13
count: 4 容器的大小:13按照我的想法,count 应该是一直递增的,因为我在方法内调用自身时传递时的是已经改变的 count值 ,可是不知道为什么count会一会大一会小,,有知道的高手帮忙下,不胜感激..数据库是的article表结构为 atricle字段:id ,pid ,rootid ,title ,cont,pdate,isleaf
我定义的Atricle类是为了方便用的.只有简单的gets和sets
现在我想使用分页的方法,把贴子分页显示,,显示时,调用的是下面的方法<%!
private static void tree (List<Article> articles , Connection conn, int id ,int grade,int startPos,int endPos,int count){
//DB是我写的一个连接数据库的一个小CLASS, 这里没有问题.
String sql = "select * from article where pid = "+ id ;
Statement stmt = DB.getStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql) ;
try {
while ( rs.next() ){
count ++ ;
Article a = new Article() ;
if( count >= startPos && count <= endPos){
a.setId(rs.getInt("id"));
a.setPid(rs.getInt("pid"));
a.setRootId(rs.getInt("rootid"));
a.setTitle(rs.getString("title"));
a.setPDate(rs.getTimestamp("pDate"));
a.setGrade(grade + 1) ;
articles.add(a);
System.out.println("count: "+count+" 容器的大小:"+articles.size());
}
//
if ( !a.isLeaf() && count <= endPos){ //又是一个递归!!!!
tree(articles,conn,rs.getInt("id"),grade+1,startPos,endPos,count);
System.out.println("count: "+count+" 容器的大小:"+articles.size());
}
}
//System.out.println("count:"+count+"startPOs:"+startPos+"endPos:"+endPos);
}catch(SQLException e){
e.printStackTrace();
}
}
%>
在调用时,参数传递为:
tree(articles,conn,0,0,startPos,endPos,0);其中statrPos =0 endPos = 4 就是想一页显示5条记录,下面是我后台打印的结果:count: 1 容器的大小:1
count: 2 容器的大小:2
count: 3 容器的大小:3
count: 4 容器的大小:4
count: 4 容器的大小:4
count: 3 容器的大小:4
count: 4 容器的大小:5
count: 4 容器的大小:5
count: 2 容器的大小:5
count: 3 容器的大小:6
count: 4 容器的大小:7
count: 4 容器的大小:7
count: 3 容器的大小:7
count: 4 容器的大小:8
count: 4 容器的大小:8
count: 1 容器的大小:8
count: 2 容器的大小:9
count: 3 容器的大小:10
count: 3 容器的大小:10
count: 4 容器的大小:11
count: 4 容器的大小:11
count: 2 容器的大小:11
count: 3 容器的大小:12
count: 3 容器的大小:12
count: 4 容器的大小:13
count: 4 容器的大小:13按照我的想法,count 应该是一直递增的,因为我在方法内调用自身时传递时的是已经改变的 count值 ,可是不知道为什么count会一会大一会小,,有知道的高手帮忙下,不胜感激..数据库是的article表结构为 atricle字段:id ,pid ,rootid ,title ,cont,pdate,isleaf
我定义的Atricle类是为了方便用的.只有简单的gets和sets
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货