while循环中的createclasstree方法的返回值没接收,所以无论调用多少次,只是最外边的返回语句返回了值。而且还有疑问:只给str变量赋值为new String() ?

解决方案 »

  1.   

    String str="";
    if rs1.next()) 
      str=rs1.getString("title") + createclasstree(rs1.getInt("id"));
    else
      return str;
      

  2.   

    int i = 0;
    ArrayList str = new ArrayList();//这两句为全局变量
    public ArrayList createclasstree(int parentid) throws Exception 

            sql = "Select parent,id,title From classlist Where parent="+ parentid;
            ResultSet rs1 = dbconn.executeQuery(sql);
            //str[i]=new String();
            while (rs1.next()) 
      {
                    str.add(rs1.getString("title"));
                         createclasstree(rs1.getInt("id"));

               }
            rs1.close();
            return str;
    }
      

  3.   

    while (rs1.next()) 
    {
       str += rs1.getString("title");
       str += createclasstree(rs1.getInt("id"));
    }
      

  4.   

    用StringBuffer不要用String,StringBuffer是线程安全的while (rs1.next()) 
    {
       str.append(rs1.getString("title"));
       str.append(createclasstree(rs1.getInt("id")));
    }
      

  5.   

    字符不多String就可以了,另外一个id有多少parent,用while直接将title连接不好吧,是不是要有连接符,如-
    public String createclasstree(int parentid) throws Exception 

            String sql = "Select parent,id,title From classlist Where parent="+ parentid;
            ResultSet rs1 = dbconn.executeQuery(sql);
            String str="";
            while (rs1.next()) 
      {
                    str=rs1.getString("title");
                         str=str+createclasstree(rs1.getInt("id"));

               }
            rs1.close();
            return str;
    }
      

  6.   

    为什么while (rs1.next()),只能循环一层呢?也就是rs1循环回来后没有记忆了!
      

  7.   

    看来你还没懂。因为你没有保存createclasstree方法的返回值,所以即使递归成功,也没有用。