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