public class Test {
private static void tree(Set<Article> articles, Connection conn,int id,int grade){
String sql = "select * from article where pid = "+id;
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
try{
while(rs.next()){
Article a = new Article();
a.setId(rs.getInt("id"));
a.setPid(rs.getInt("pid"));
a.setRootId(rs.getInt("rootid"));
a.setTitle(rs.getString("title"));
a.setLeaf(rs.getInt("isleaf") == 0?true:false);
a.setGrade(grade);
a.setPdate(rs.getTimestamp("pdate"));
articles.add(a);
if(!a.isLeaf())
tree(articles,conn,a.getId(),grade+1);
}
}catch (SQLException e){
e.printStackTrace();
}
}
public static void main(String args[])
{
Set<Article> articles = new HashSet<Article>();
Connection conn = DB.getConn();
tree(articles,conn,0,0);
DB.close(conn);
System.out.println(articles.size());
}
}数据库中的10条数据的isleaf分别是1 1 0 1 0 1 0 0 1 0
我debug运行后发现,在递归到第三次isleaf为0时再判断rs.next竟然为true,为什么?我认为执行一条SQL查询应该只查询出一行数据,resultset仅有一行数据,所以第2次判断rs.next应该为false了.请各位大仙指教.
解决方案 »
- 100分,在线等,急急急...
- jsp跳转异常
- 请教关于JMF传输的问题
- 如何动态实例化一个类(通过代码来实现)
- jtable问题,填写表格时,点击其他组件,如按钮,表格中的值没有保存,需要用Enter才能保存,如何做到离开表格时,实现Enter功能?
- 关于Collections.sort,请教这段代码是什么意思?
- JNI的使用中GetIntField()参数怎么写?
- import外部类和look&feel问题
- 使用JDK编译servlet不能通过,是不是要把servlet.jar这个文件放到classpath这个环境变量中?
- 这段代码单独连接数据库没问题,当加入到applet里面不知为什么有问题,在线等
- JAVA关闭360tray进程
- 关于notify 和notifyAll方法
String sql = "select * from article where pid = "+id;
这是你的sql语句,你是根据pid来查询article表的值的,跟isleaf有什么联系?
你应该表达清楚你要表达的问题,要实现什么样的效果,再说a.isLeaf()中的isLeaf()函数是什么样的,它实现了一个什么功能,你都应该把源码贴出来