From 1000 goto 2000



Membuat struktur JTREE melalui database

pertama kita membuat tabel stu_info menggunakan query sebagai berikut :

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`)
)

lalu isi tabelnya :

sekarang kita membuat struktur tampilan JTREE ,buat class 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(275, 300);
setLocation(300, 100);
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);
}
}

lalu running programmnya, dan akan tampil seperti berikut :


Download Sourcecode Here *

Sumber :

1. http://www.roseindia.net/java/example/java/swing/retrieving-jtree-structure-from.shtml


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: