java基于数据库建树

java基于数据库建树,第1张

重新帮你写了数据库相关的语句。

但是你必须得做响应的修改才能使用。

import java.sql.Connection

import java.sql.PreparedStatement

import java.sql.ResultSet

import java.sql.SQLException

import javax.swing.JFrame

import javax.swing.JScrollPane

import javax.swing.JTree

import javax.swing.tree.DefaultMutableTreeNode

public class SwingTree extends JFrame {

public static class MyTreeNode extends DefaultMutableTreeNode {

public MyTreeNode(Object obj) {

super(obj)

}

public MyTreeNode(MyTreeNode parent, Object object) {

super(object)

parent.add(this)

}

public MyTreeNode findOrAddChild(Object obj) {

for (int i = 0 i < getChildCount() i++) {

MyTreeNode node = (MyTreeNode) getChildAt(i)

if (node.valueEquals(obj)) {

return node

}

}

return new MyTreeNode(this, obj)

}

public boolean valueEquals(Object obj) {

return getUserObject().equals(obj)

}

}

private MyTreeNode rootNode

public static void main(String[] args) {

new SwingTree()

}

public SwingTree() {

this.setTitle("Swing Tree")

this.setSize(800, 600)

this.setDefaultCloseOperation(EXIT_ON_CLOSE)

initNode()

JTree tree = new JTree(rootNode)

this.add(new JScrollPane(tree))

this.setVisible(true)

}

private void initNode() {

rootNode = new MyTreeNode("清华大学")

try {

String sql = "select * from student_t"

PreparedStatement preStmt = getConnection().prepareStatement(sql)

ResultSet query = preStmt.executeQuery()

while (query.next()) {

String college = query.getString("college")

String department = query.getString("department")

String clazz = query.getString("clazz")

String student = query.getString("student")

student += " " + query.getString("phone")

addNode(college, department, clazz, student)

}

} catch (SQLException e) {

e.printStackTrace()

}

}

public Connection getConnection() {

return null

}

public void addNode(String college, String department, String clazz, String student) {

rootNode.findOrAddChild(college)

.findOrAddChild(department)

.findOrAddChild(clazz)

.findOrAddChild(student)

}

}

1、准备表结构及对应的表数据

a、表结构:

create table TB_TREE

(

CID NUMBER not null,

CNAME VARCHAR2(50),

PID NUMBER //父节点

)

b、表数据:

insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0)

insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1)

insert into tb_tree (CID, CNAME, PID) values (3, '广东省', 1)

insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1)

insert into tb_tree (CID, CNAME, PID) values (5, '广州市', 3)

insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3)

insert into tb_tree (CID, CNAME, PID) values (7, '海珠区', 5)

insert into tb_tree (CID, CNAME, PID) values (8, '天河区', 5)

insert into tb_tree (CID, CNAME, PID) values (9, '福田区', 6)

insert into tb_tree (CID, CNAME, PID) values (10, '南山区', 6)

insert into tb_tree (CID, CNAME, PID) values (11, '密云县', 2)

insert into tb_tree (CID, CNAME, PID) values (12, '浦东', 4)

2、TreeNode对象,对应tb_tree

public class TreeNode implements Serializable {

private Integer cid

private String cname

private Integer pid

private List nodes = new ArrayList()

public TreeNode() {

}

//getter、setter省略

}

3、测试数据

public class TreeNodeTest {

@Test

public void loadTree() throws Exception{

System.out.println(JsonUtils.javaToJson(recursiveTree(1)))

}

/**

* 递归算法解析成树形结构

*

* @param cid

* @return

* @author jiqinlin

*/

public TreeNode recursiveTree(int cid) {

//根据cid获取节点对象(SELECT * FROM tb_tree t WHERE t.cid=?)

TreeNode node = personService.getreeNode(cid)

//查询cid下的所有子节点(SELECT * FROM tb_tree t WHERE t.pid=?)

List childTreeNodes = personService.queryTreeNode(cid)

//遍历子节点

for(TreeNode child : childTreeNodes){

TreeNode n = recursiveTree(child.getCid())//递归

node.getNodes().add(n)

}

return node

}

}

输出的json格式如下:

{

"cid": 1,

"nodes": [

{

"cid": 2,

"nodes": [

{

"cid": 11,

"nodes": [

],

"cname": "密云县",

"pid": 2

}

],

"cname": "北京市",

"pid": 1

},

{

"cid": 3,

"nodes": [

{

"cid": 5,

"nodes": [

{

"cid": 7,

"nodes": [

],

"cname": "海珠区",

"pid": 5

},

{

"cid": 8,

"nodes": [

],

"cname": "天河区",

"pid": 5

}

],

"cname": "广州市",

"pid": 3

},

{

"cid": 6,

"nodes": [

{

"cid": 9,

"nodes": [

],

"cname": "福田区",

"pid": 6

},

{

"cid": 10,

"nodes": [

],

"cname": "南山区",

"pid": 6

}

],

"cname": "深圳市",

"pid": 3

}

],

"cname": "广东省",

"pid": 1

},

{

"cid": 4,

"nodes": [

{

"cid": 12,

"nodes": [

],

"cname": "浦东",

"pid": 4

}

],

"cname": "上海市",

"pid": 1

}

],

"cname": "中国",

"pid": 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10052766.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存