package TreeDemo;
import java.beans.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.sun.corba.se.pept.transport.Connection;
import com.sun.tools.javac.util.List;
public class TreeTest {
/**
* @param args
*/
int j=0;
TreeNode tn1=new TreeNode();
static Connection con;
static Statement stmt;
static ResultSet rs;
public TreeNode buildRoot()
{
TreeNode Root=new TreeNode();
Root.setId(0);
Root.setCaption("Root");
TreeNode a=new TreeNode();
a.setId(1);
a.setCaption("A");
TreeNode a1=new TreeNode();
a1.setId(2);
a1.setCaption("A1");
TreeNode a2=new TreeNode();
a2.setId(3);
a2.setCaption("A2");
TreeNode a11=new TreeNode();
a11.setId(4);
a11.setCaption("A11");
TreeNode a12=new TreeNode();
a12.setId(5);
a12.setCaption("A12");
TreeNode b=new TreeNode();
b.setId(6);
b.setCaption("B");
TreeNode b1=new TreeNode();
b1.setId(7);
b1.setCaption("B1");
TreeNode b2=new TreeNode();
b2.setId(8);
b2.setCaption("B2");
TreeNode b11=new TreeNode();
b11.setId(9);
b11.setCaption("B11");
TreeNode b12=new TreeNode();
b12.setId(10);
b12.setCaption("B12");
a1.addTreeNode(a11);
a1.addTreeNode(a12);
a.addTreeNode(a1);
a.addTreeNode(a2);
Root.addTreeNode(a);
b1.addTreeNode(b11);
b1.addTreeNode(b12);
b.addTreeNode(b1);
b.addTreeNode(b2);
Root.addTreeNode(b);
return Root;
}
public static ArrayList getAll()
{
ArrayList result=new ArrayList();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Tree";
con=(Connection) DriverManager.getConnection(url,"sa","");
stmt = (Statement) ((java.sql.Connection) con).createStatement();
rs=((java.sql.Statement) stmt).executeQuery("select * from treetest");
while(rs.next())
{
TreeNode n=new TreeNode();
n.setId(rs.getInt("id"));
n.setCaption(rs.getString("caption"));
n.setParentid(rs.getInt("parentid"));
n.setSortedid(rs.getInt("sortedid"));
result.add(n);
}
return result;
}
catch(Exception e)
{
System.out.print("error");
}
return result;
}
//取当前接点直接子接点
public static ArrayList getSubing(TreeNode node) throws ClassNotFoundException
{
ArrayList result=new ArrayList();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Tree";
con=(Connection) DriverManager.getConnection(url,"sa","");
stmt = (Statement) ((java.sql.Connection) con).createStatement();
rs=((java.sql.Statement) stmt).executeQuery("select * from treetest where parentid="+node.getId()+ " order by id");
while(rs.next())
{
TreeNode n=new TreeNode();
n.setId(rs.getInt("id"));
String s2="|--"+rs.getString("caption");
String s=new String();
int level=rs.getInt("sortedid");
n.setSortedid(level);
//System.out.println(n.getLevel());
for(int i=1;i<level;i++)
{
s=s+" ";
}
n.setCaption(s+s2);
n.setSortedid(rs.getInt("sortedid"));
n.setParentid(rs.getInt("parentid"));
result.add(n);
}
} catch (SQLException e) { e.printStackTrace(); }
return result;
}
//getTree方法递归生成树
static ArrayList re1=new ArrayList();
public static ArrayList getTree(TreeNode node,List treeList) throws ClassNotFoundException
{
if(!node.isLeaf())
{
ArrayList ss=getSubing(node);
for(int i=0;i<ss.size();i++)
{
TreeNode n=(TreeNode)ss.get(i);
re1.add(n);
getTree(n,treeList);
}
}
else
{
re1.add(node);
}
return re1;
}
/* public TreeNode BuildRootFromTable()
{
}*/
public void displayTree(TreeNode tn)
{
System.out.println(tn.getCaption().toString());
for(int i=0;i<tn.getSubNodeList().size();i++)
{
tn1=(TreeNode)tn.getSubNodeList().get(i);
displayTree(tn1);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeTest treetest=new TreeTest();
treetest.displayTree(treetest.buildRoot());
System.out.println(TreeTest.getAll());
}
}
class TreeNode
{
private int id;
private String caption;
private ArrayList subNodeList=new ArrayList();
private int parentid;
private int sortedid;
public String getCaption() {
return caption;
}
public boolean isLeaf() {
// TODO Auto-generated method stub
return false;
}
public void setCaption(String caption) {
this.caption = caption;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getParentid() {
return parentid;
}
public void setParentid(int parentid) {
this.parentid = parentid;
}
public int getSortedid() {
return sortedid;
}
public void setSortedid(int sortedid) {
this.sortedid = sortedid;
}
public ArrayList getSubNodeList() {
return subNodeList;
}
public void setSubNodeList(ArrayList subNodeList) {
this.subNodeList = subNodeList;
}
public void addTreeNode(TreeNode tn)
{
this.subNodeList.add(tn);
}
public void deleteTreeNode(TreeNode tn)
{
this.subNodeList.remove(tn);
}
}
写了许多方法,但是不会调用了,buildRoot()是自己建立死的一个树,这个通过调用displayTree()可以显示的,但是 我要把数据库中的数据通过树的形式显示出来不会了。哪位大虾帮忙完善完善 万分感谢!
上传的是数据库备份文件 ,在线等 邮箱chenxiaoyun927@hotmail.com