新手 问个递归的小程序

zoubiantianxia666 zoubiantianxia666 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
private void delete (Connection conn, int id, boolean isLeaf ) {

//delete all the children
if(!isLeaf) {
Statement stmt = DB.createStatement(conn);
String sql = " select * from article where pid = " + id;
ResultSet rs = DB.getResultSet(stmt,sql);
try{
while (rs.next()) {
delete(conn,rs.getInt("id"), rs.getInt("isLeaf")==0);
}
} catch(SQLException e) {
e.printStackTrace();
} finally{
DB.close(rs);
DB.close(stmt);
}
}

//delete it self
String sql = " delete from article where id = " + id;
DB.executeUpdate(conn,sql);
}

然后调用此函数delete(conn,id,isLeaf)
简单说明一下,这是删除帖子和他的子帖的小程序,我不太明白的是当帖子不是叶子节点时会执行if 后面的语句知道获得他的叶子贴,然后删除。可是我不明白的是我觉得这个小程序只能删除叶子贴,然而不是叶子贴的一概删除不了,因为通过调用会一直执行if后面的帖子直到拿到叶子贴,可其他的非叶子贴拿不到啊。。。 事实上所有帖子都能删除,但我不知道我想的哪里错了,请大家帮忙
...全文
评论 9
收藏 已收藏
分享
返回顶部
zoubiantianxia666
Web 开发 81,108 成员 341,721 内容