递归输出树形节点

1.脚本

CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`parentid` int(11) DEFAULT NULL,
`parentName` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

/*Data for the table `a` */

insert into `a`(`id`,`name`,`parentid`,`parentName`) values (1,'中国',0,NULL),(2,'四川',1,'中国'),(3,'北京',1,'中国'),(4,'成都',2,'四川'),(5,'朝阳区',3,'北京');

2.DBUtil.java

package tree; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { //取得数据库连接 public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("驱动程序没有找到" + e.getMessage()); } try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "123"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("获得连接出错!"); } return conn; } }

3.节点类

package tree; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; public class TreeNode { private int id; private String name; private int parentId; private String parentName; public TreeNode(int id) { this.id = id; if (!init()) { this.id = -1; } } // 初始化节点 public boolean init() { String sql = "select * from a where id=" + getId(); Connection conn = null; Statement st = null; ResultSet rs = null; boolean flag = false; try { conn = DBUtil.getConnection(); st = conn.createStatement(); rs = st.executeQuery(sql); if (rs.next()) { this.parentId = rs.getInt("parentid"); this.name = rs.getString("name"); this.parentName = rs.getString("parentname"); flag = true; } rs.close(); st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); return false; } return flag; } // 是否可以展开 public boolean canExpand() { boolean flag = false; String sql = "select * from a where parentid=" + getId(); Connection conn = null; Statement st = null; ResultSet rs = null; try { conn = DBUtil.getConnection(); st = conn.createStatement(); rs = st.executeQuery(sql); if (rs.next()) { flag = true; } rs.close(); st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return flag; } // 取得子节点列表 public TreeNode[] getChildren() { String sql = "select id from a where parentid=" + getId(); Connection conn = null; Statement st = null; ResultSet rs = null; Vector vData = new Vector(); TreeNode[] children = null; try { conn = DBUtil.getConnection(); st = conn.createStatement(); rs = st.executeQuery(sql); while (rs.next()) vData.add("" + rs.getInt("id")); children = new TreeNode[vData.size()]; for (int i = 0; i < vData.size(); i++) { int id = Integer.parseInt((String) vData.get(i)); children[i] = new TreeNode(id); } rs.close(); st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); return null; } return children; } // 递归生成xml格式的字符串 public static String getXMLString(TreeNode treeNode) { StringBuffer content = new StringBuffer(); //输出当前节点 content.append("<node label=/"" + treeNode.getName() + "/">"); //如果有子节点的话,递归输出子节点的内容 if (treeNode.canExpand()) { TreeNode[] elmts = treeNode.getChildren(); for (int i = 0; i < elmts.length; i++) content.append(getXMLString(elmts[i])); } content.append("</node>"); return content.toString(); } //测试 public static void main(String[] args) { TreeNode treeNode = new TreeNode(1);//根节点 String result=getXMLString(treeNode); System.out.println(result); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getParentId() { return parentId; } public void setParentId(int parentId) { this.parentId = parentId; } public String getParentName() { return parentName; } public void setParentName(String parentName) { this.parentName = parentName; } public void setId(int id) { this.id = id; } public int getId() { return id; } }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值