Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Create a tree in JAVA using the data from DB

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create a tree in JAVA using the data from DB

    I have data from the DB as follows.

    ID PARENT ID NAME
    5344 (null) Qwest Washington
    83688 5344 Anderson
    83690 83688 Anderson_Const
    83712 83688 Larson_Cable
    83726 83712 SEAIR
    83714 83688 Larson_Const
    83748 83714 SECN2
    83750 83714 SECN3
    83692 5344 Butler
    83694 83692 Cini_Const
    83696 83692 Cini_Loop
    83834 83696 TANW1
    83836 83696 TANW2
    83702 83692 Flores
    83704 83702 Flores_Design
    83730 83702 SEBG2
    83732 83702 SEBG4
    83734 83702 SEBG5

    Data given above will change for each user. i want to write generic java method which construct tree and return output as jsonString like below,

    {id:'0',text:'Org Node',expanded: true, children:
    [
    { id:'Org1', text: "node1", leaf: true },
    {id:'Org2', text: "node2", expanded: true, children: [
    { id:'Org21', text: "node2-1", leaf: true },
    { id:'Org22', text: "node2-2", leaf: true }
    ]
    },
    {id:'Org3', text: "node3", leaf:true },
    {id:'Org4', text: "node4", children: [
    { id:'Org41', text: "node4-1", leaf: true },
    { id:'Org42', text: "node4-2", leaf: true }
    ]
    },
    {id:'Org5', text: "node-5", leaf: true }
    ]
    }

    Please give me a generic concept to make tree in JAVA.


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Create a tree in JAVA using the data from DB

    Hello.
    You can use composition. It is similar to self-referential structures in C language.

    Syed.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a tree in JAVA using the data from DB

    Thanks. But i am completely stuck. i dont know how to start. i have the data. some of them have child and some of the dont have. while fetching data from DB, it will not come in order. among 50 records last record might become a child of root. So how can i set the child of each node and make it as JSON String. Just give me an example.

Similar Threads

  1. How to Use JTree to create a File System Viewer Tree
    By JavaPF in forum Java Swing Tutorials
    Replies: 0
    Last Post: March 15th, 2011, 05:05 AM
  2. How to Use JTree to create a File System Viewer Tree
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: March 15th, 2011, 05:05 AM
  3. Replies: 2
    Last Post: January 21st, 2011, 02:07 AM
  4. Create a tree from list of strings
    By ayri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2010, 04:32 AM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM

Tags for this Thread