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

数据库中的表结构:

id        pid

1         0

2         1

3         1

4         2

6         2

5         4

需要生成这样一个树结构,选取combo,需要查看哪个根节点。此时我选取的是combo中的0,所以显示如下。但是这是我手动添加的,界面如下:

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

2.选取某个数字作为查看时的父节点,触发cre_tree()生成一个tree

3.tree的内容通过get_list()方法得到,并存放在List> tree_list中,tree_list的内容就为:

[1, 2, 4, 5]

[1, 2, 6, null]

[1, 3, null, null]

现在手动添加的代码是死的:

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

。。。。。。

#################################################

请教大家如何将这种结构的内容添加到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("请选择父节点");

//获取可能的父节点,并添加到下拉框

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();

}

//返回所有可能的父节点

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值