title: How to get Parent-Child relationship by using recursive function in java
body: <p>I want to get parent-child relation array by using recursive function in java.
  I tried some ways to display I want but it looks something wrong. pls give me some suggestions and guidelines. </p>
  
  <p>I have 3 tables in my db; dept, company, depttree</p>
  
  <p>dept</p>
  
  <pre><code>dept_cd company_cd
  100     0017
  101     0017
  102     0017
  103     0017
  200     0017
  201     0017
  202     0017
  300     0017
  301     0017
  302     0017
  303     0017
  304     0017
  999     0017
  </code></pre>
  
  <p>company</p>
  
  <pre><code>company_cd  
  0017
  0018
  </code></pre>
  
  <p>depttree</p>
  
  <pre><code>dept_cd (Parent)    child_dept_cd
  100                 101
  100                 999
  200                 201
  200                 202
  300                 301
  300                 302
  101                 102
  102                 103
  302                 303
  302                 304
  </code></pre>
  
  <p><strong>I want to display (in console) as the followings.</strong></p>
  
  <pre><code>[100]
  [100,101]
  [100,101,102]
  [100,101,102,103]
  [100,999]
  [200]
  [200,201]
  [200,202]
  [300]
  [300,301]
  [300,302]
  [300.302,303]
  [300,302,304]
  </code></pre>
  
  <p>In my program, I wrote as the followings.</p>
  
  <pre><code>    ArrayList&lt;ArrayList&lt;String&gt;&gt; g_nodes = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
  
      functionA() {
         String p_DeptCd = functionB(g_ssp.g_gp.getParam("company_cd"), 0);
         //e.g: company_cd = 0017, 0 means '0017 does not have parent'
      }
      functionB(String x_dept_cd, int x_flag){
          PmsSql p_sql = new PmsSql(g_ssp);
      if (x_flag == 0) {
          p_sql.setField("dept.dept_cd, dept.dept_cd_nk, dept.dept_nm, dept.dept_disp_nm");
          p_sql.setTable("dept INNER JOIN company ON dept.company_cd = company.company_cd");
          p_sql.addWhere("dept.company_cd = ?");
          p_sql.addWhere("dept.dept_cd NOT IN (SELECT child_dept_cd FROM depttree)");
          p_sql.addWhereValue("company_cd", x_dept_cd);
          p_sql.setOrder("dept.disp_order desc");
      }
      if (x_flag == 1) {
          p_sql.setField("dept.dept_cd, dept.dept_disp_nm");
          p_sql.setTable("dept INNER JOIN depttree ON dept.dept_cd = depttree.child_dept_cd");
          p_sql.setWhere("dept.dept_cd_nk IS NOT NULL");
          p_sql.addWhere("depttree.dept_cd = ?");
          p_sql.addWhereValue("dept_cd", x_dept_cd);
          p_sql.setOrder("dept.disp_order desc");
      }
      p_sql.execQuery();
      while (p_sql.next()) {
          String p_dept_cd = p_sql.getString("dept_cd");
          ArrayList&lt;String&gt; p_childArr = new ArrayList&lt;String&gt;();
  
          if(x_flag == 1){
              p_childArr.add(x_dept_cd);
          }
  
          p_childArr.add(p_dept_cd);
          g_nodes.add(p_childArr);
          System.out.println("g_nodes = "+g_nodes);
          functionB(p_dept_cd, 1);
      }
      return null;
      }
  </code></pre>
  
  <p>but it displays like that.</p>
  
  <pre><code>g_nodes = [[100], [100, 999], [100, 101], [101, 102], [102, 103], [200], [200, 201], [200, 202], [300], [300, 301], [300, 302], [302, 303], [302, 304]]
  </code></pre>
  
  <p><strong>I'm very sorry for my long descriptions.</strong> Thanks in advance.</p>