数据库中的表结构:

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;

}

}

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

  1. vb 读取mysql所有表名_VB 读取ACCESS数据库中所有表名和指定表字段名.doc

    VB 读取ACCESS数据库中所有表名和指定表字段名 托束显疫面绰么蚂扛沁米衅居鸳辗熊踌薄舱杂们帖婉珍抓津担庐撕枪呻头胀亨短宛溅锭语氏遣搀赠摩紧茧综逾颂备呛庞枷脓移厚醛仕恬浸值胶碟亦短咳腥钥捎魄朗氟商 ...

  2. 查询前几条记录SQL在不同数据库中的用法

    13,(1)查询前几条记录SQL在不同数据库中的用法; 1. ORACLE SELECT * FROM TABLE1 WHERE ROWNUM<=N 2. INFORMIX SELECT FIR ...

  3. 通过JSP网页连接MySQL数据库,从MySQL数据库中读出一张表并显示在JSP网页中

    1.安装所需软件 ①安装java和tomcat,建立JSP网页最基础的软件 ②安装MySQL数据库(下载地址:https://www.mysql.com/) ③安装Navicat Premium来查看 ...

  4. 什么是数据库中的一对一关系?

    什么是数据建模中的一对一关系?如何在数据库中实现这种关系?本文中的示例将回答这些问题. 数据建模中的实体(表)之间有三种类型的关系: 一对多关系(也表示为 1:M). 多对多关系 (M:N). 一对一 ...

  5. 读取azkaban数据库中的longblob数据类型

    介绍 当需要读取azkaban数据库中的longblob数据类型的数据时,可以使用Java连接Mysql数据库的方式进行读取. 代码 代码如下: public String getErrorLog() ...

  6. mysql升级后将旧数据迁移到新 数据库中

    mysql升级后将旧数据迁移到新 数据库中 1.将原来数据库进行导出成.sql文件 例:StudentSql.sql 2.进入新数据库的bin目录下 复制bin目录 3.cmd进入bin目录下(需管理 ...

  7. 关系型数据库中所谓的“关系”是指( )。

    A.各个记录中的数据彼此间有一定的关联关系 B.是指数据模型符合满足一定条件的二维表格式 C.某两个数据库文件之间有一定的关系 D.表中的两个字段有一定的关系 B :对 关系型数据库中所谓的" ...

  8. mysql 查询编码转换_字符集介绍及mysql数据库编码转换

    一.字符集介绍: 1.ASCII ASCII是英文American Standard Code for Information Interchange的缩写,美国标准信息交换代码是由美国国家标准学会( ...

  9. mysql 查询和修改组合_## 超详细MySQL常用语句,增删查改

    ## MySQL常用语句,增删查改,安装配置mysql服务 ***#新手博客,应届生,谢谢支持哟 记得点赞关注哟*** ***-----sql常见命令:--------*** 安装服务:mysqld ...

最新文章

  1. Javascript匿名函数
  2. linux下makefile使用
  3. python列表常用方法
  4. 使用nginx缓存服务器上的静态文件
  5. C API可使用的函数归纳
  6. 3.2 神经网络概述-机器学习笔记-斯坦福吴恩达教授
  7. 2016全国大学生计算机设计大赛,2016年“第9届中国大学生计算机设计大赛”闭幕...
  8. 计算机组成原理课程设计a,计算机组成原理课程设计报告.doc
  9. 全民直播,半年“用云量”暴涨五倍
  10. 【把P2P进行到底:讲述Jxta的故事(2)】
  11. BS7799,ISO17799与ISO27001的关系
  12. 前端H5面试题Js: 数组的常用方法有哪些?
  13. 会计专业与计算机专业结合复合型,我国会计电算化的现状、问题及对策
  14. 全网最易懂的Flink背压问题,看不懂你打我~
  15. 如何搭建一个自己图床网站
  16. MCAL中MCU的配置
  17. 海思3559A的一些工具探索尝试
  18. css动画让箭头上下跳动
  19. 超级详细的 shell编程知识讲解 —— 第二部分(全文3万多字,看完之后不想成为高手也难!)
  20. 2007年1月 讨论:人为什么活着(人活着就是为了能够光荣的死去)

热门文章

  1. 在Ubuntu18上搭建K8s集群(1.19.3)
  2. Delphi常用关键字用法详解
  3. 对现有的所能找到的DDOS代码(攻击模块)做出一次分析----其他(攻击方式)篇
  4. 事件通知方式实现的重叠I/O模型
  5. 互斥体CMutex的使用
  6. 云计算的背后到底是什么?
  7. 那些做了十年的老开发都去哪了?
  8. 双指针解决力扣两/三数之和问题
  9. Revvel如何将视频转码速度提升几十倍?
  10. JVM之常用基础参数详解