Share on Google+Share on Google+

Retrieving JTree structure from database

This example shows how to retrieving data from the database and how to add the data in to JTree.

Retrieving JTree structure from database


This example shows how to retrieving data from the database and how to add the data in to JTree.

JTree : JTree is used for viewing data in a list. Lists are good for displaying simple lists of information from which the user can make single or multiple selections. In list You can hide different  levels of data in the tree, including the root, allowing the display to collapse and expand various parts of the tree.

In this program we  are using stu_info table. The table can be created by the query as below:

CREATE TABLE `stu_info` ( 
`ID` int(11) NOT NULL auto_increment, 
`Name` varchar(20) default NULL, 
`Address` varchar(20) default NULL, 
`Phone` varchar(15) default NULL, 

Now populate the table with the data as show below.

Now in the program below, access the database and retrieve the data from the table. This data can be used to populate the data for the node of the tree. Now nodes can be added to the tree. The program below "JTreeStructure" demonstrates all the steps required to create tree retrieving the data from the database.

Here is the full code for "":

import java.awt.*;
import java.sql.*;
import java.util.*;

import javax.swing.*;
import javax.swing.tree.*;

public class JTreeStructure extends JFrame {

  Connection con = null;
  Statement st = null;
  ResultSet rs = null;

  public static void main(String args[]) throws Exception {
  new JTreeStructure();

  public JTreeStructure() throws Exception {

  super("Retrieving data from database ");

  String driver = "com.mysql.jdbc.Driver";
  String url = "jdbc:mysql://";
  String db = "Student";

  ArrayList list = new ArrayList();
  list.add("Roll Numbers");

  con = DriverManager.getConnection(url + db, "root""root");

  try {
  String sql = "Select * from stu_info";

  st = con.createStatement();
  rs = st.executeQuery(sql);

  while ( {
  Object value[] rs.getString(1), rs.getString(2),
  rs.getString(3), rs.getString(4) };
  catch (Exception e) {

  Object hierarchy[] = list.toArray();

  JFrame frame = new JFrame();
  Container content = getContentPane();

  DefaultMutableTreeNode root = processHierarchy(hierarchy);
  JTree tree = new JTree(root);
  content.add(new JScrollPane(tree), BorderLayout.CENTER);

  private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
  DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
  DefaultMutableTreeNode child;
  for (int i = 1; i < hierarchy.length; i++) {
  Object nodeSpecifier = hierarchy[i];
  if (nodeSpecifier instanceof Object[]) // Ie node with children
  child = processHierarchy((Object[]) nodeSpecifier);
  else {
  child = new DefaultMutableTreeNode(nodeSpecifier)// Ie Leaf
  return (node);

Running the above program displays the output as below:

Output :

Download Source Code



Posted on: July 7, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Retrieving JTree structure from database   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Alinda Chandra Mondal
February 29, 2012

One word "FANTASTIC" post! I have been helped all the way by rose india for the past 3 years of java programming. Thank you from the core of my heart Rose India!...Sometimes IP address is not helpful for connection if your IP address is assigned by DHCP. Try using "localhost". It works....
Kasun Bandara
June 25, 2012

Hi, I want to group many objects at many tree nodes. do you know how to do that ?