日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql查询父子关系树_swt 生成树[读取Mysql数据库中的父子关系表]

發(fā)布時(shí)間:2024/2/28 数据库 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql查询父子关系树_swt 生成树[读取Mysql数据库中的父子关系表] 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)庫中的表結(jié)構(gòu):

id??????? pid

1???????? 0

2???????? 1

3???????? 1

4???????? 2

6???????? 2

5???????? 4

需要生成這樣一個(gè)樹結(jié)構(gòu),選取combo,需要查看哪個(gè)根節(jié)點(diǎn)。此時(shí)我選取的是combo中的0,所以顯示如下。但是這是我手動添加的,界面如下:

1.通過get_count()方法,把所有Pid加載到combo里面;

2.選取某個(gè)數(shù)字作為查看時(shí)的父節(jié)點(diǎn),觸發(fā)cre_tree()生成一個(gè)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中,謝謝!

問題補(bǔ)充:界面在附件中~~

問題補(bǔ)充: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é)點(diǎn)");

//獲取可能的父節(jié)點(diǎn),并添加到下拉框

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é)點(diǎn)

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。