title: java: Make multi level dynamic jtree from database table
body: <p>i have make a database table like the following, but the rows can be more</p>
  
  <pre><code>id  Name     parent   
  1  color      null        
  2  red        1        
  3  white      1         
  4  green      1         
  5  dark       4        
  6  light      4 
  </code></pre>
  
  <ul>
  <li>To retrieve the table from database i already written the code following. i search all over the internet but unable to find the solution that's what i found, i am successful to make a connection with Db, and print the resultSet values using while loop , but unable to make jtree, i mean i make jtree but its not working as it should do.</li>
  </ul>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public class TreeDemo extends javax.swing.JFrame {
  
      /**
       * Creates new form TreeDemo
       */
      String Connection;
      Connection conn;
      Statement pst = null;
      ResultSet rs = null;
  
      DefaultTreeModel model;
      
      LinkedHashMap&lt;Integer, ObjClass&gt; lhm = new LinkedHashMap&lt;&gt;();
  
      public TreeDemo() {
          initComponents();
  
          model = (DefaultTreeModel) TreePro.getModel();
  
           // retrieve Jtree From dataBase
          try {
              Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
              String url = "jdbc:oracle:thin:@localhost:1521:XE";
              conn = DriverManager.getConnection(url, "Hr", "Hr");
              System.out.println("Connection Established");
  
              Statement stmt = conn.createStatement();
              ResultSet rs = stmt.executeQuery("SELECT * FROM contacts order by node_id");
              System.out.println("id  name    job" + rs);
  
              // Retrieve rs from DataBase
  
              
              while (rs.next()) {
                  int id = rs.getInt("node_Id");
                  String name = rs.getString("node_Name");
                  String par = rs.getString("node_Parent");
                  int lvl = rs.getInt("node_Level");
                 System.out.println(id + "   " + name + "    " + par + " " + lvl);
       
     
       String parentId = rs.getString("node_Id");  
       String parentName = rs.getString("node_Name");
       String paren = rs.getString("node_Parent");
       int treeLvl = rs.getInt("node_Level");
  
       Map&lt;String, DefaultMutableTreeNode&gt; parentsMap
       = new HashMap&lt;&gt;();
          DefaultTreeModel tbl = (DefaultTreeModel) TreePro.getModel();
     DefaultMutableTreeNode root = (DefaultMutableTreeNode)tbl.getRoot();
     
    
  DefaultMutableTreeNode Node1 = parentsMap.get("Root");
  
  
  
  
         Node1 = new DefaultMutableTreeNode( paren );
         
         parentsMap.put(parentId, Node1 );
         root.add(Node1); 
   
       DefaultMutableTreeNode parent = parentsMap.get(parentName);
  
         parent = new DefaultMutableTreeNode( parentName );
         parentsMap.put(parentId, parent );
         Node1.add(parent);
  
            
              
              }   
  
          } catch (Exception e) {
  
              System.out.println("connection error " + e);
          }
   
      }}}</code></pre>
  </div>
  </div>
  </p>
  
  <p>Now it's creates jtree like this</p>
  
  <pre><code>Root
   Null
    -color
   color
    -red
   color
    -white
   color
    -green
   green
    -dark
   green
    -light
  </code></pre>
  
  <p>It should show jtree like this:</p>
  
  <pre><code> Root
    _color
      -red
      -white
      _grenn
        -dark
        -light
  </code></pre>
  
  <p>Note: the jtree can be multilevel.</p>