Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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, 
PRIMARY KEY (`ID`) 
)

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 "JTreeStructure.java":

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://192.168.10.59:3306/";
    String db = "Student";

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

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

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

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

      while (rs.next()) {
        Object value[] rs.getString(1), rs.getString(2),
            rs.getString(3), rs.getString(4) };
        list.add(value);
      }
    catch (Exception e) {
      System.out.println(e);
    }
    rs.close();
    st.close();
    con.close();

    Object hierarchy[] = list.toArray();

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = getContentPane();

    DefaultMutableTreeNode root = processHierarchy(hierarchy);
    JTree tree = new JTree(root);
    content.add(new JScrollPane(tree), BorderLayout.CENTER);
    setSize(275300);
    setLocation(300100);
    setVisible(true);
  }

  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
      }
      node.add(child);
    }
    return (node);
  }
}

Running the above program displays the output as below:

Output :

Download Source Code

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
get url
Two Dimensional Array
How to deploy a java p
java strings
how to use email valid
Expression Language
jdbc html jsp
showMessage
value change listener
jboss portal
icon a JButton
pb tree implementati
covert uppercase to lo
Eclipse 3.4 EJB
struts e file jpeg
how to capture multipl
Get Array List in a JS
create table using jsp
microsoft word
try example
spring applicationcont
jsp drop down menu
jasper report
Copy one or more files
how to write hql in da
ArrayList
jstl
wap to print a trriang
PHP Database Related O
login jsp
Photoshop Drawing Drag
passing array
delete data to text fi
rational rose
how to capture multipl
how to declare an arra
JSF panelGrid render
add in excel
assertNull
Java Count Vowels
Applet JSP Programing
wait
selection model
jstl list size
java combo box
java: read string arra
Rational Method Compos
how to put eclipse plu
word
HTML Image Maps Client
Hibernate program
validating username an
date validation in jav
eclipseme1.7.3 downloa
velocity rules engine
Java Swing Center a Di
Java Mysql
JSF panelGrid Tag
how to upload image ur
how to user JSpinner t
java.util.Calendar
Junit Eclipse
example of struts with
insert fields blob
multiple ajax combo
ajax on spring
code for Login Applica
ASP Ad Management Rota
crystal reports
Bryce
garbage collection in
struts-blank.war
action:scope=
logical relational ope
callable
jdk-6u1-windows-i586-p
find out factors of nu
Flash Games Location D
Photoshop Cheese Tutor
Action Forms
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.