Amit Chauhan
Tree View with database
2 Answer(s)      4 years and 9 months ago
Posted in : Java Beginners

Hi,

I'm working with Swing.I have to construct a SplitPane with JTree on the leftside and its details which is stored in the database on the right side of the pane.
For ex:

Network
|
|Node1
| |
| |Slot1
| |
| |Line1
| |Line2
|Node2

I have stored the Node Details,Slot details,line details in database(separate table) like Node_name,id..

Now, my problem is, If I click the Node "Node1", it has to display the database details for that corresponding Node in that right side of the pane. Similarly for other nodes also..

How to write code for this? How to connect JTree with database.(MySql).

Give 1 suggestion for this plz...

Thanks
Advertisement
View Answers

August 3, 2010 at 6:01 PM


Hi Friend,

Try the following code:

import javax.swing.*;
import javax.swing.tree.*;
import java.sql.*;
import java.awt.*;
import java.util.*;
import javax.swing.event.*;


public class JTreeDatabase {

public static void main(String[] args){
JFrame frame = new Database();
frame.show();
}
}
class Database extends JFrame implements TreeSelectionListener{
JTextArea description;
public Database(){
setSize(400, 300);
DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Students", true);
DefaultMutableTreeNode n1 = new DefaultMutableTreeNode("1");
DefaultMutableTreeNode n2 = new DefaultMutableTreeNode("2");
DefaultMutableTreeNode n3 = new DefaultMutableTreeNode("3");
DefaultMutableTreeNode n4 = new DefaultMutableTreeNode("4");
DefaultMutableTreeNode n5 = new DefaultMutableTreeNode("5");
DefaultMutableTreeNode n6 = new DefaultMutableTreeNode("6");
parent.add(n1);
parent.add(n2);
parent.add(n3);
parent.add(n4);
parent.add(n5);
parent.add(n6);

JTree tree = new JTree(parent);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);
description = new JTextArea();
JSplitPane innerPane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tree, description);
getContentPane().add(innerPane, "Center");

}
public void valueChanged(TreeSelectionEvent se) {
JTree tree = (JTree) se.getSource();
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
String selectedNodeName = selectedNode.toString();
try{
Class.forName( "com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test";, "root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from student where rollNo='"+selectedNodeName+"'");
String data="";
while (rs.next()) {
data=rs.getString("name")+" "+rs.getString("address")+" "+rs.getString("age");
}
description.setText(data);
}
catch(Exception e){}

}


}

Thanks


March 13, 2012 at 7:24 PM


i want tree view structuer total table data will display so pleas help me

Now, my problem is, If I click the Node "folder", it has to display database table details for that corresponding table in that right side of the pane. Similarly for other tablesalso..










Related Tutorials/Questions & Answers:
Tree View with database - Java Beginners
Tree View with database   Hi, I'm working with Swing.I have... in the database on the right side of the pane. For ex: Network | |Node1... I have stored the Node Details,Slot details,line details in database
tree view
tree view  display a tree view in flex?nodes will be in the form
Advertisements
tree view - Java Beginners
tree view   I m using swing for the treeview in my application and also the database(Hsqldb). I want to connect the tree view with the database fields. If u want more clarification ,let me know. Thanks
tree view - Java Beginners
tree view  Hello Deepak, Thanks for your help last time regarding java. I have another problem, Actually I want a tree view in my application which will display the files
tree view - Java Beginners
tree view  I want to use tree view in my example code to display data in hierarchical form. how ?? Thanks   Hi friend, Please visit the following links: http://www.roseindia.net/java/example/java/swing
Populate Menus In Tree View - Struts
" as prefix.In the same way i am thinking to bring the menus in a tree view in my application "is it possible to bring menu that is stored in a database in a tree...Populate Menus In Tree View  Hi all, i am writing one simple
JSP tree view - JSP-Servlet
JSP tree view  Hi... In my project i need to display the data from database in the form of tree structure... i.e. like horizontal binary tree I've to dynamically get the values... Can you plssssss suggest me how
Reg: Tree view in Struts using ajax - Struts
Reg: Tree view in Struts using ajax  HI all, Can you figure out the tree and treenode attribute belong to which .jar file or tld ? Example... the example on it. Example for Tree view in Struts to visit this link.. http
where do I find the fix for tree view catalog not expanding and collapsing correctly for roseindia shopping cart 1.1
where do I find the fix for tree view catalog not expanding and collapsing correctly for roseindia shopping cart 1.1  where do I find the fix for tree view catalog not expanding and collapsing correctly for roseindia shopping
view
view  what is the use view in database
Tree
Tree  print("code sample");1) Write Java code to create the following tree using new Tree state- ments: 1
Balance multi-way tree implimentation reating database - Java Beginners
database like Access,MySQL.As we know database is based on Balance multi-way tree...Balance multi-way tree implimentation reating database  I am doning a project in which I have to create a database product in java where user can
Creating Database using B+Tree in Java - Java Beginners
Creating Database using B+Tree in Java  I'm doing a project in which I have to create an Object Oriented Database using B+Tree in Java..It is not a Database connectivity..I have to create a seperate Database(like Oeacle)my-self
view data from database using drop down list
view data from database using drop down list  hi i want to view the data from database by selecting a value in a drop down list. for an example drop down list have picture element.when click it select pictures from the database
Save profile and image to mysql database, and view the image in another jsp page
Save profile and image to mysql database, and view the image in another jsp page  Pls. need help in saving the profile info with the image in mysql database.. some basic code pls in jsp... thanks in advance
view
view  hi iam writing some struts application by using dyanafalidator form in place of actionform bean classes i can enter data by using some jsp file and it will insert to database by using business logic. now my requirement
How to view database for user when they login in netbeans and mysql?
How to view database for user when they login in netbeans and mysql?  I create a web page where when user login they have to fill in a form, after... database with jdbc connection. thanks for your help
Retrive data from database and perform binary tree operations on that data in jsp or java
Retrive data from database and perform binary tree operations on that data... from database dynamically and perform operations on that. Just like calculate Left and Subbinary tree node count. Check balanced tree of particular Id ,unbalaced
Nitobi Tree
coding and effort. Nitobi Tree adds just like folder view in Windows...Nitobi Tree       Nitobi Tree Nitobi Tree, the Ajax component of User Interface Suite is used
Spanning Tree
Spanning Tree  hii, What is a spanning Tree?   hello, A spanning tree is a tree associated with a network. All the nodes of the graph appear on the tree once. A minimum spanning tree is a spanning tree organized so
binary tree
binary tree  how to count no. of nodes in a binary tree for mlm if it complet tree or incomplet tree in php using mysql db
What is View?
one or more existing database tables or other views. In effect every view...What is View?  What is View?   Hi, A SQL View is a virtual table, which is based on SQL SELECT query. Essentially a view is very close
Binary tree
Binary tree  hii, What is binary tree?   hello, A binary tree is a tree in which every node has exactly two links i.e left and right link
Binary tree
Binary tree  a. Construct a method to implement a binary tree using an array. b. Implement the binary tree to store numbers in sorted order
i want to protect mysql database table which user can not view to table and can not copy my table till then us
i want to protect mysql database table which user can not view to table and can not copy my table till then us  i want to protect mysql database table which user can not view to table and can not copy my table till then user do
i want to protect mysql database table which user can not view to table and can not copy my table till then us
i want to protect mysql database table which user can not view to table and can not copy my table till then us  i want to protect mysql database table which user can not view to table and can not copy my table till then user do
binary tree
binary tree  can a binary tree be implemented with out comparing...://www.roseindia.net/java/java-get-example/java-binary-tree-code.shtml http://www.roseindia.net/java/java-get-example/java-binary-tree-insert.shtml
tree in java
tree in java  Can you help me to draw the tree? First we assign the parent node. when child node(x) come we must check the condition. there are three condition such as x<5,5<=x<10 and 10<=x<15 after that we have
FLex Tree
FLex Tree  I want to change the node name in flex tree my code is: Company:XML = <Element name ="Element" code="400"> <{newChild} name ={newChild1} code="400"/> ; where comapny is dataprovide for tree. I want
How to design a tree, design a tree, a tree
How to design a tree       Be alert to learn a simple technique to make a tree example... as you need. Tree: Choose this "4c2228" color and Pen Tool (P key
tree in java
tree in java  Can you help me to draw the tree? First we assign the parent node. when child node(x) come we must check the condition. there are three... average of children. Like following tree. (3+7+12)/3=7(root