根据数据库如何建树,各位有没有这方面的代码或提示!谢谢!
根据数据库如何建树,各位有没有这方面的代码或提示!谢谢!
june37@163.net
问题点数:50、回复次数:3Top
1 楼d80(今天没事做)回复于 2003-06-03 11:17:05 得分 50
这是我写的关于用递归形成数装的文件,你看看吧
import java.sql.*;
import java.io.*;
import java.util.*;
public class TreeTest {
int layer=-1;//\u8BBE置\u5C42\u6570,默任\u4E3A-1
String space="";
public Connection getConnection() throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.216.0.2:1521:ORCL","dms2","dms2");
return conn;
}
/**
* \u4F20入自己的id\u53F7,\u9012\u5F52\u663E示出\u6811
* @param id
* @throws Exception
*/
public void tree(String id) throws Exception{
boolean hasChild=false;
boolean hasNextBrother=false;
layer=-1;
space="";
try{
Connection conn =getConnection();
Statement stmt=conn.createStatement();
String sql1="select id,fid,name from test_tree where fid='"+id+"'";
ResultSet rs1=stmt.executeQuery(sql1);
while(rs1.next()){
//得到自己所在的\u5C42\u6570
getLayer(rs1.getString("id"));
//判\u65AD是否有子\u8282\u70B9
hasChild=hasChild(rs1.getString("id"));
//判\u65AD是否有下一\u4E2A兄弟\u8282\u70B9
hasNextBrother=hasNextBrother(rs1.getString("id"));
//\u753B出子\u8282\u70B9的\u56FE片
drawNode(hasChild,hasNextBrother);
//\u753B出子\u8282\u70B9
System.out.println(space+rs1.getString("name"));
//\u9012\u5F52\u8C03用
tree(rs1.getString("id"));
}
stmt.close();
conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* 返回自己所在的\u5C42\u6570
* @param id 自己的id\u53F7
* @throws Exception
*/
public void getLayer(String id) throws Exception{
Connection conn =getConnection();
Statement stmt=conn.createStatement();
String sql="select id,fid from test_tree where id='"+id+"'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
layer++;
getLayer(rs.getString("fid"));
}
stmt.close();
conn.close();
}
/**
*
* @param id
* @return 判\u65AD是否有子\u8282\u70B9,有返回true,\u6CA1有有返回false
* @throws Exception
*/
public boolean hasChild(String id) throws Exception{
Connection conn =getConnection();
Statement stmt=conn.createStatement();
String sql="select id,fid from test_tree where fid='"+id+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
return true;
}
stmt.close();
conn.close();
return false;
}
/**
*
* @param id
* @return 判\u65AD是否有下一\u4E2A兄弟
* @throws Exception
*/
public boolean hasNextBrother(String id) throws Exception{
Connection conn =getConnection();
Statement stmt=conn.createStatement();
String sql="select id from test_tree where fid in (select fid from test_tree where id='"+id+"')";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
if(rs.getString("id").equals(id))
break;
}
if(rs.next()){
return true;
}
stmt.close();
conn.close();
return false;
}
/**
*
* @param hasChild
* @param layer
* @return
*/
public String drawNode(boolean hasChild,boolean hasNextBrother){
for(int i=0;i<layer-1;i++){
space+=" ";/*
if(hasChild==true&&hasNextBrother==true){
space+="| ";
}
if(hasChild==true&&hasNextBrother==false){
space+="| ";
}
if(hasChild==false&&hasNextBrother==true){
space+="| ";
}
if(hasChild==false&&hasNextBrother==false){
space+="| ";
}*/
}
if(hasChild){//hasChild
if(hasNextBrother){//hasNextBrother
space+="★";
}else{
space+="※";
}
//space+="-";
}else{//hasnochild
if(hasNextBrother){//hasNextBrother
space+=" ";
}else{
space+="■";
}
//space += "+";
}
return space;
}
public static void main(String[] args){
try {
TreeTest treeTest=new TreeTest();
Connection conn=treeTest.getConnection();
Statement stmt=conn.createStatement();
String sql2 ="select id,fid,name from test_tree where id='0'";
ResultSet rs2 = stmt.executeQuery(sql2);
if (rs2.next()) {//\u753B出根\u8282\u70B9
System.out.println(rs2.getString("name"));
}
treeTest.tree(rs2.getString("id"));
stmt.close();
conn.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}Top
2 楼rgbahnh(秋风)回复于 2003-06-04 10:59:16 得分 0
厉害,佩服Top
3 楼june37(june)回复于 2003-06-06 15:28:01 得分 0
楼上的兄台,我看得不是很明白,可以分析一下流程吗?表结构是什么样子的啊!
你这个树是什么样子的啊!谢谢!Top




