新手 问个递归的小程序
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后面的帖子直到拿到叶子贴,可其他的非叶子贴拿不到啊。。。 事实上所有帖子都能删除,但我不知道我想的哪里错了,请大家帮忙