日期:2014-05-20  浏览次数:20804 次

递归方法
下面的一个方法是删除一个目录树参数指定该目录的id及是否是叶子节点(0为叶子节点返回true)
具体实现是有递归先删除该节点的叶子节点,最后删除本身(执行的delete语句)。
我的问题是:删除叶子节点(子目录)并没有调用delete语句怎么就删除了呢??


Java code

public static void categoryDelete(int id, boolean isLeaf) {
        Connection conn =  null ;
        Statement stmt = null ;
        ResultSet rs = null ;
        String sql_child = "select * from category where pid = " + id;
        conn = DB.getConn();
        stmt = DB.getStmt(conn);
        rs = DB.executeQuery(stmt, sql_child);
        if(!isLeaf){
            try {
                while (rs.next()) {
                     categoryDelete(rs.getInt("id"), rs.getInt("leaf")==0);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
            DB.closeRs(rs);
            DB.closeStmt(stmt);
          }
      }
      //删除该目录
    String sql_self = "delete from category where id = "+ id ;
    DB.executeUpdate(conn, sql_self);
    }



------解决方案--------------------
递归的方法只要一步一步走下去就清楚了,能调试的话单步执行一下吧,不要仅凭主观
------解决方案--------------------
删除的语句写在递归的方法里面,一层层找下去,当找到叶子节点的时候就自动执行删除的语句了。
------解决方案--------------------
//主要是这里 id
String sql_self = "delete from category where id = "+ id ;
因为这里的ID是是递归传过来的ID 直接就删除了。

------解决方案--------------------
探讨
//主要是这里 id
String sql_self = "delete from category where id = "+ id ;
因为这里的ID是是递归传过来的ID 直接就删除了。


------解决方案--------------------
一步一步走下去,一层层找下去,
String sql_self = "delete from category where id = "+ id ; 

------解决方案--------------------
因为你最后删除了节点了。