Anda di halaman 1dari 9

HOME

BLOG

ANDROID

PRIMEFACES

ASK QUESTION

CORE JAVA

JASPER REPORT

SWING

PROGRAMMING TUTORIALS (VIDEO)

POPULATE JTREE FROM MY SQL


DATABASE
Posted on November 29, 2012

by admin

FOLLOW ME!

comments

TThhiiss eexxaam ppllee sshhoow


wss h oow
w t oo aadddd M
MyySSQ
QLL d a t a t oo
J TTrreeee..
A
Att f i r sstt c r eeaattee t hhee d a t aabbaassee aanndd ttaab lleess a s
ffoolllloow
wss::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

RECENT POSTS

CREAATTEE DDAATTAABBAASSEE IF NOT EXISTS jtree_category;


Bangla Core Java
CREAATTEE TTAABBLLEE `category` (
Tutorial-10
`catid` iinntt(10) unsigned NOT NULL
Bangla Core Java
`catname` vvaarrcchhaarr(45) NOT NULL,
Tutorial-09
PRI
IMMAARRYY KKEEYY (`catid`)
);
Bangla Core Java
Tutorial-08
INSEERRTT IINNTTOO `category` (`catid`,`catname`)
Bangla Core Java
(1,'Monitor'),
(2,'Mouse'),
Tutorial-07
(3,'Keyboard');
Bangla Core Java
Tutorial-06
CREAATTEE TTAABBLLEE `subcategory` (
`scatid` iinntt(10) unsigned NOT NULL
`catid` iinntt(10) unsigned NOT NULL
`scatname` vvaarrcchhaarr(45) NOT NULL
PRI
IMMAARRYY KKEEYY (`scatid`),
RECENT COMMENTS
KEY
Y `FK_subcategory_1` (`catid`),
CON
NSSTTRRAAIINNTT `FK_subcategory_1` FFOORREEIIGGNN
);
developerdesk:

22
23
24
25
26
27
28

host it on google or
amazon cloud as i
INSEERRTT IINNTTOO `subcategory` (`scatid`,`catid`,`scatn
did
this ... It
(1,1,'Samsung'),
rajuahmed:
(2,2,'A4Tech'),
depends on you
(3,1,'Philips'),
Just carry on. ...
(4,2,'Sonic'),
Robinkhan: Thank
(5,3,'Perfect');
you Sir ...

N
Noow
w c rreeaattee aa jjaavvaa pprroojjeecctt a t nneettbbeea n aanndd c r eeaat ee
tth ee f oolll o w
wiinngg c llaasssseess::
D
Daat aabbaassee..jjaavvaa //for making the connection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

CATEGORIES
packkaaggee util;
impoorrtt java.sql.Connection;
impoorrtt java.sql.DriverManager;
Android (19)
publliicc ccllaassss Database {
ppuubblliicc ssttaattiicc Connection getConnection()
{ (37)
Core Java
ttrryy {
Core Java
Class.forName("com.mysql.jdbc.Driver
Connection con = DriverManager.getCo
Basic by
("jdbc:mysql://localhost:330
Example (4)
"root","yourdbpass"

Swing (27)
rreettuurrnn con;
}
Android like
ccaattcchh(Exception ex) {
Toast using
System.out.println(
Java Swing
rreettuurrnn nnuullll;
}
Backup &
}
Restore

MySQL
ppuubblliicc ssttaattiicc vvooiidd close(Connection con)
{
ttrryy {
Database
con.close();
Using
}
ccaattcchh(Exception ex) {
Java-Complet
}
e Solution
}
copylibs
doesn't

D
DiissppllaayyC
Caatteeggoor yy..jjaavvaa //to display the JTree

support the
"excludeFro
mCopy"
packkaaggee display;
attribute
impoorrtt java.sql.*;
Custom
impoorrtt java.util.ArrayList;
JButton with
impoorrtt javax.swing.tree.DefaultMutableTreeNode;
impoorrtt javax.swing.tree.DefaultTreeModel; Gradient
impoorrtt util.Database;
Color

//Basically this is a JFrame


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

publliicc ccllaassss DisplayCategory eexxtteennddss


Connection con = nnuullll;
Statement stm = nnuullll;

Custom
JDialog with
Expandable

Details Pane
ppuubblliicc DisplayCategory() {
Display
initComponents();
Image on
pop_tree();//call to populate the JTree
JFrame from
}
//System generated code
MySQL

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

@SuppressWarnings("unchecked"
Dynamic
// <editor-fold defaultstate="collapsed" desc
JTable:
pprriivvaattee vvooiidd initComponents() {
Display Sum
jPanel1 = nneeww javax.swing.JPanel();of Selected
jScrollPane1 = nneeww javax.swing.JScrollPan
cat_tree = nneeww javax.swing.JTree();Row
Dynamically
setDefaultCloseOperation(javax.swing.Wind
add row to
setTitle("Product List");
JTable
jScrollPane1.setViewportView(cat_tree);
Event handle
using
javax.swing.GroupLayout jPanel1Layout =
jPanel1.setLayout(jPanel1Layout); implements
jPanel1Layout.setHorizontalGroup( Event handle
jPanel1Layout.createParallelGroup(jav
using inner
.addGroup(jPanel1Layout.createSequent
class
.addGap(18, 18,
.addComponent(jScrollPane1,
javax
How
to add
.addContainerGap(
JPopupMenu
);
jPanel1Layout.setVerticalGroup(
to JFrame
jPanel1Layout.createParallelGroup(jav
and image as
.addGroup(jPanel1Layout.createSequent
background
.addGap(23, 23,
.addComponent(jScrollPane1,ofjavax
JFrame
.addContainerGap(
JMenuBar
);
Background
javax.swing.GroupLayout layout =
Image
getContentPane().setLayout(layout);
Example
layout.setHorizontalGroup(
JSplitPane
layout.createParallelGroup(javax.swin
.addGroup(layout.createSequentialGrou
with
.addContainerGap()
ClosableTab
.addComponent(jPanel1, javax.swin
.addContainerGap(javax.swing.Grou
Application

);
JTextPane
layout.setVerticalGroup(
Line Number
layout.createParallelGroup(javax.swin
.addGroup(layout.createSequentialGrou
JToolBar
.addContainerGap()
Background
.addComponent(jPanel1, javax.swin
Image
.addContainerGap(javax.swing.Grou
);
Example

P
Po
oppuul a t ee
pack();
J TTrreeee ffrroom
m
}// </editor-fold>
M
MyySSQ
QLL
@SuppressWarnings("CallToThreadDumpStack"
D
a
t
a
bbaas ee
ppuubblliicc ffiinnaall vvooiidd pop_tree() {
ttrryy {
Print
JTextArea
ttrryy {
using
con = Database.getConnection();
stm = con.createStatement();
PrinterJob in
} ccaattcchh (Exception ex) {
Java
ex.printStackTrace();
Resize image
}
ArrayList list = nneeww
and upload
list.add("Category List"

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

String sql = "SELECT * from category"


to MySQL db
using Java
ResultSet rs = stm.executeQuery(sql);

Swing
wwhhiillee (rs.next()) {
Shaking
Object value[] = {rs.getString(
JFrame With
list.add(value);
}
Blinking
Object hierarchy[] = list.toArray();
Effect If
DefaultMutableTreeNode root = process
Login
DefaultTreeModel treeModel =
Unsuccessful
cat_tree.setModel(treeModel);
Simple
} ccaattcchh (Exception e) {
Slideshow
}
using Card
}
Layout
Simple Splash
@SuppressWarnings("CallToThreadDumpStack"
ppuubblliicc DefaultMutableTreeNode processHierarch
Screen with
DefaultMutableTreeNode node =
Progress Bar
ttrryy {
iinntt ctrow = 0;
in Java Swing
iinntt i = 0;
Some
ttrryy {
Important
ttrryy {
Tips About
con = Database.getConnection(
Several
stm = con.createStatement();
Components
} ccaattcchh (Exception ex) {
ex.printStackTrace(); in Swing
}
Status
Bar
String sql = "SELECT catid,
catna
Example for
ResultSet rs = stm.executeQuery(s
JFrame
wwhhiillee (rs.next()) {

Use
of Bangla
ctrow = rs.getRow();
}
Localization
String L1Nam[] =
in Java Swing
String L1Id[] =
Using
ResultSet rs1 = stm.executeQuery(
wwhhiillee (rs1.next()) {
JDateChoose
L1Nam[i] = rs1.getString(
r From Your
L1Id[i] = rs1.getString(
NetBeans
i++;
}
Palette
DefaultMutableTreeNode child, gra

Working
with
ffoorr (iinntt childIndex =
JList
child = nneeww
node.add(child);
Write The
String sql2 =
Contents of a
ResultSet rs3 = stm.executeQu
wwhhiillee (rs3.next()) {
JFrame to a
grandchild =
Text File and
child.add(grandchild);
Display
}
}
Recorded
Data to a
} ccaattcchh (Exception ex) {
JTable
ex.printStackTrace();
}
copylibs doesn't

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

} ccaattcchh (Exception e) {
}
}

rreettuurrnn (node);

support the
"excludeFromC
opy" attribute
Create Jar File

ppuubblliicc ssttaattiicc vvooiidd main(String args[])Programmatica


{
java.awt.EventQueue.invokeLater( lly

How to add
ppuubblliicc vvooiidd run() {
Notification to
nneeww DisplayCategory().setVisible(
}
System Tray in
});
java
}
Play Sound on
// Variables declaration - do not modify
pprriivvaattee javax.swing.JTree cat_tree; Button Click in
pprriivvaattee javax.swing.JPanel jPanel1;
Java
pprriivvaattee javax.swing.JScrollPane jScrollPane1;
Some Tips on
// End of variables declaration
}
Working with
Date in Java
TThhiiss i ss t h ee o uuttppu t ::
Sorting dynamic
array

Free Projects (2)


Hibernate (5)
Jasper report (7)
JSF 2.0 (13)
JSP & Servlet (9)
JSTL 1.1 (4)
NetBeans (2)
PrimeFaces (8)
Programming
Tutorial(video)
(10)

JTree Displaying tree from MySQL db

Software (3)
Spring (1)
Struts2 (1)
Tomcat (2)

TAG CLOUD

Related Posts

JSplitPane with

Simple CRUD Using

ClosableTab Application

JSF2+Hibernate
Integration and MySQL

META

Register

Upload and display

Jsp, Servlet, JSTL and

Log in
Entries RSS
Comments RSS

image from MySQL db

MySQL Simple CRUD

WordPress.org

using PrimeFaces

Application

ARCHIVES
CALENDAR

September 2015
M
M TT W
W TT FF SS SS

1 2 3 4 5 6
Sample Login app in

Login and Registration

7 8 9 10 11 12 13

android using Servlet

Example in JSP with

and JSON Parsing

Session

14 15 16 17 18 19 20

TTw
weeeett

S
Shhaarree

21 22 23 24 25 26 27
4

LLiikkee

28 29 30
Jun

S
Shhaarree

admin
Hello! I am Md. Abdul

ARCHIVES
Select Month

Bari; founder and admin


of javaknowledge;
currently providing
training as a J2EE faculty
in The Computers Ltd.
Dhaka, Bangladesh under

CONNECT ON
FACEBOOK

an IT Scholarship project
provided by IDB-BISEW. I

Javaknowledge
3,053 likes

am a self learner and


passionate about training
and writing. I am always
trying my best to share
my knowledge through

LLiikkee P
Paaggee
Be the first of your friends to like this

my blog.
More Posts - Website
Follow Me:

Tweets

FFoolllloow
w

13 Aug

tagged with JTree, mysql, Populate JTree


Swing

javaknowledge
@java_knowledge

JAVA Training With


Live Project
fb.me/3AONeTAtp

10,381` Total Views

LEAVE A REPLY
Your email address will not be published. Required
fields are marked *
Name *

11 Aug

javaknowledge
@java_knowledge

Tweet to
@java_knowledge

SITE RANK

Email *

Website
BLOG STATISTICS

User Online: 3
1 = eight
Comment

Today Visitor:
1,137
Yesterday Visitor:
823
Total Visit:
2,241,304
Total Posts: 108
Total Comments:

Post Comment

6 COMMENTS
Pradeep

March 6, 2013 1:51 pm

Thank you very much sir..!! I


appreciate your work and i have been
looking for this everywhere, and Im
novel for java. Your tutorial is much
helped me. Could you please explain me how to
add another level for this tree
Eg
Category List
-Monitor
Samsung
Korean Samsung
Chinese Samsung
Philips
-Mouse
A4Tech
Sonic
-Keyboard
Perfect
Ive stuck on my project and need to solve this
problem ASAP.
I would really appreciate your help.
Thank You
-Pradeep
PS (Please dont mind my English , coz im not
native English speaker):)
Reply
gp

January 31, 2014 6:28 am

Pradeep, i have the same


question, do you have a solution ?

Reply

530
Total Users:
16,395
Last Post Date:
June 7, 2015

Pradeep

March 6, 2013 1:54 pm

Appreciate if you can explain how


database should be altered and
codings, bcoz im really novel to java.
Thanks in advance
Reply
Arnab Saha

August 9, 2013 1:40 pm

Nice job

Reply
a_omess

June 17, 2014 9:39 am

Thank you, this really helped my


work.

Reply
madhusara

December 20, 2014 1:48 am

thanx sir for all example

Reply

Journey to the way of Java...

zeeDynamic Theme

Anda mungkin juga menyukai