mysql查询父子关系树_swt 生成树[读取Mysql数据库中的父子关系表]
數(shù)據(jù)庫中的表結(jié)構(gòu):
id??????? pid
1???????? 0
2???????? 1
3???????? 1
4???????? 2
6???????? 2
5???????? 4
需要生成這樣一個樹結(jié)構(gòu),選取combo,需要查看哪個根節(jié)點。此時我選取的是combo中的0,所以顯示如下。但是這是我手動添加的,界面如下:
1.通過get_count()方法,把所有Pid加載到combo里面;
2.選取某個數(shù)字作為查看時的父節(jié)點,觸發(fā)cre_tree()生成一個tree
3.tree的內(nèi)容通過get_list()方法得到,并存放在List> tree_list中,tree_list的內(nèi)容就為:
[1, 2, 4, 5]
[1, 2, 6, null]
[1, 3, null, null]
現(xiàn)在手動添加的代碼是死的:
Tree tree = new Tree(shell, SWT.BORDER);
tree.setBounds(30, 36, 382, 201);
List> tree_list = new ArrayList>(GetList.get_list(st));
TreeItem temp = new TreeItem(tree,SWT.NONE);
temp.setText(st);
TreeItem temp_1 = new TreeItem(temp,SWT.NONE);
temp_1.setText(tree_list.get(0).get(0));
TreeItem temp_2 = new TreeItem(temp_1,SWT.NONE);
temp_2.setText(tree_list.get(0).get(1));
TreeItem temp_3 = new TreeItem(temp_2,SWT.NONE);
temp_3.setText(tree_list.get(0).get(2));
。。。。。。
#################################################
請教大家如何將這種結(jié)構(gòu)的內(nèi)容添加到tree中,謝謝!
問題補充:界面在附件中~~
問題補充:CreateTree.java:
public class CreateTree {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
CreateTree window = new CreateTree();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
final Combo combo = new Combo(shell, SWT.READ_ONLY);
combo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String st = combo.getText();
cre_tree(st);
}
});
combo.setBounds(108, 7, 138, 20);
Label label = new Label(shell, SWT.NONE);
label.setBounds(30, 10, 72, 20);
label.setText("請選擇父節(jié)點");
//獲取可能的父節(jié)點,并添加到下拉框
List conlist = new ArrayList(getcontent.get_count());
for(int i = 0; i < conlist.size(); i++){
combo.add(conlist.get(i));
}
}
public void cre_tree(String st){
{
Tree tree = new Tree(shell, SWT.BORDER);
tree.setBounds(30, 36, 382, 201);
List> tree_list = new ArrayList>(GetList.get_list(st));
TreeItem temp_1 = null;
TreeItem temp_2 = null;
TreeItem temp_3 = null;
TreeItem temp_4 = null;
TreeItem temp_5 = null;
TreeItem temp_6 = null;
TreeItem temp_7 = null;
TreeItem temp_8 = null;
TreeItem temp_9 = null;
TreeItem[] item_list = {temp_1,temp_2,temp_3,temp_4,temp_5,temp_6,temp_7,temp_8,temp_9};
TreeItem temp = new TreeItem(tree,SWT.NONE);
temp.setText(st);
item_list[0] = temp;
List str_save = new ArrayList();
for(int i = 1; i < tree_list.get(0).size(); i++){
item_list[i] = new TreeItem(item_list[i-1],SWT.NONE);
String child = tree_list.get(0).get(i);
item_list[i].setText(child);
str_save.add(child);
}
int cnt = 0;
for(int j = 1; j < tree_list.size(); j++){
int count = 1;
for(int k = 1; k < tree_list.get(j).size(); k++){
String str_find = tree_list.get(j).get(k);
if(str_save.indexOf(str_find) == 0){
System.out.println("0");
count++;
} else {
item_list[count] = new TreeItem(item_list[count-1],SWT.NONE);
item_list[count].setText(str_find);
str_save.add(str_find);
}
}
}
}
}
}
########################################################################
getcontent.java:
public class getcontent {
public static List get_count(){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "alex456";
String content = null;
List list_return = new ArrayList();
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
Statement statement = conn.createStatement();
String sql = "SELECT DISTINCT pid FROM TEST_TREE";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
content = rs.getString("pid");
list_return.add(content);
}
rs.close();
conn.close();
} catch(ClassNotFoundException e){
System.out.println("Sorry, can't find the Driver!");
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
//返回所有可能的父節(jié)點
return list_return;
}
}
################################################################
GetList.java
public class GetList {
public static List> get_list(String pid){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "alex456";
String str = null;
List> list_tree = new ArrayList>();
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
Statement statement = conn.createStatement();
String sql = "SELECT t1.pid as id,t1.id as id1, t2.id as id2, t3.id as id3, t4.id as id4, t5.id as id5, t6.id as id6, t7.id as id7, t8.id as id8, t9.id as id9 from ";
sql = sql + "test_tree t1 left join test_tree t2 on t1.id = t2.pid left join test_tree t3 on t2.id = t3.pid left join test_tree t4 on t3.id = t4.pid ";
sql = sql + "left join test_tree t5 on t4.id = t5.pid left join test_tree t6 on t5.id = t6.pid left join test_tree t7 on t6.id = t7.pid ";
sql = sql + "left join test_tree t8 on t7.id = t8.pid left join test_tree t9 on t8.id = t9.pid where t1.pid = ";
sql += pid;
sql += " order by isnull(id9),isnull(id8),isnull(id7),isnull(id6),isnull(id5),isnull(id4),isnull(id3),isnull(id2),isnull(id1)";
ResultSet rs = statement.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData();
int colCount = rsmd.getColumnCount();
while(rs.next()){
List list_mid = new ArrayList();
for(int l = 1; l < colCount + 1; l ++){
str = rs.getString(l);
if(str != null){
list_mid.add(str);
}
}
list_tree.add(list_mid);
}
rs.close();
conn.close();
} catch(ClassNotFoundException e){
System.out.println("Sorry, can't find the Driver!");
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
for(int i = 0; i < list_tree.size(); i++){
System.out.println(list_tree.get(i));
}
return list_tree;
}
}
總結(jié)
以上是生活随笔為你收集整理的mysql查询父子关系树_swt 生成树[读取Mysql数据库中的父子关系表]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: b g opencv读入的图片 r_Op
- 下一篇: mysql重新载入my.cnf_怎么重新
