字符串:
LDAP://gz.cvte.cn/CN=张三,OU=研发中心,OU=事业部,OU=AAA有限公司,OU=C集团
LDAP://gz.cvte.cn/CN=李四,OU=研发中心,OU=BBB公司,OU=C集团
......表结构:(可以修改,反正能体现到树状结构就好了)
id
name:名称
fatherId: 父Id
type:类型(人员,公司,部门)问题:解析上述字符串,然后按照下图展示的树状形式存入数据库,数据库不限(要使用面向对象的方法)------------------------------------------------------------------------------下面是我自己写的,【但是我面向对象思维比较水,所以我没用到,而且我也还没完全实现】,希望各位大神一起来交流学习一下。都帖下自己的代码,大家一起努力咧。//DBUtil.java --用于 连接数据库 和 查询数据库中的数据 的一个类package com.toony.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;import com.toony.bean.TestBean;public class DBUtil {
private String user;
private String password;
private String host;
private String dbName;
private String port;
private String url;
private Connection conn;
private static DBUtil dbUtil = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
}
public DBUtil(String host, String dbName,String port,String user, String password) {
super();
this.user = user;
this.password = password;
this.host = host;
this.dbName = dbName;
this.port = port;
this.url = "jdbc:mysql://" + host + ":" + port + "/" + dbName;

try {
this.conn = DriverManager.getConnection(this.url, this.user, this.password);
} catch(Exception e) {
e.printStackTrace();
}
}

public static DBUtil getInstance(String host, String dbName,String port,String user, String password) {
if(dbUtil == null) {
return new DBUtil(host, dbName, port, user, password);
}
return dbUtil;
}

public Connection getConnection() {
return conn;
}

public int selParentId(String parentName, String parentType, Connection conn) {
String sql = "select id from cvt where name = '" + parentName + "' and type = '" + parentType + "'";
int parentId = 0;
ResultSet rs = null;
Statement stmt = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
parentId = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {rs.close(); rs = null;}
if(stmt != null) {stmt.close(); stmt = null;}
} catch (Exception e) {
e.printStackTrace();
}
}
return parentId;
}

public ArrayList<TestBean> selParentNode(int pid, Connection conn) {
String strSql = "select id, name, parentId, type from cvt where id = '" + pid + "'";
Statement stmt = null;
ResultSet rs = null;
ArrayList<TestBean> tempList = new ArrayList<TestBean>();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
if(rs.next()) {
TestBean tb = new TestBean();
tb.setId(rs.getInt("id")+"");
tb.setName(rs.getString("name"));
tb.setFatherId(rs.getString("parentId"));
tb.setType(rs.getString("type"));
tempList.add(tb);
}
} catch(Exception e) {
e.printStackTrace();
}
return tempList;
}

public ArrayList<TestBean> selNode(String name, String type, Connection conn) {
String strSql = "select id, name, parentId, type from cvt where name = '" + name + "' and type = '" + type + "'";
Statement stmt = null;
ResultSet rs = null;
ArrayList<TestBean> tempList = new ArrayList<TestBean>();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
if(rs.next()) {
TestBean tb = new TestBean();
tb.setId(rs.getInt("id")+"");
tb.setName(rs.getString("name"));
tb.setFatherId(rs.getString("parentId"));
tb.setType(rs.getString("type"));
tempList.add(tb);
}
} catch(Exception e) {
e.printStackTrace();
}
return tempList;
}

public boolean nodeIsExist(String name, String type, Connection conn) {
String sql = "select count(1) from cvt where name = '" + name + "' and type = '" + type + "'";
Statement stmt = null;
int counter = 0;
boolean isExist = false;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()) {
counter = rs.getInt(1);
}
if(counter > 0) {
isExist = true;
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt != null) {stmt.close(); stmt = null; }
} catch (Exception e) { e.printStackTrace(); }
}
return isExist;
}
public void closeConnection(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch(Exception e) {
e.printStackTrace();
}
}



}//Test.java --main类import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;
import org.w3c.dom.Element;import com.toony.bean.TestBean;
import com.toony.util.DBUtil;public class Test {
public static void main(String[] args) {
List<String> list = readFile("E:\\data.txt");
for (String s : list) {
inDB(s);
} } public static List<String> readFile(String path) {
File file = new File(path); // 文件路径
List<String> lists = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null; while (null != (line = br.readLine())) {
lists.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lists;
} public static void inDB(String str) {
Connection conn = null;
Statement stmt = null;
DBUtil dbUtil = DBUtil.getInstance("127.0.0.1", "test", "3306", "root","root");
conn = dbUtil.getConnection();
// String readLine;
int parentId = 0;
try {
conn.setAutoCommit(false);
stmt = conn.createStatement();
int urlLastIndex = str.lastIndexOf("/");
String tempStr = str.substring(0, urlLastIndex + 1);
str = str.substring(tempStr.length(), str.length());
String[] strs = str.split(",");
ArrayList<TestBean> tbList;
for (int i = strs.length - 1; i > -1; i--) {
// System.out.println(strs[i]);
String str1 = strs[i];
String name = str1.substring(str1.indexOf("=") + 1);
String type = str1.substring(0, str1.indexOf("=")); if (dbUtil.nodeIsExist(name, type, conn)) {
continue;
} else {
if(i == strs.length - 1) {
tbList = new ArrayList<TestBean>();
tbList = dbUtil.selNode(name, type, conn);
System.out.println(tbList.size());
if(tbList == null || tbList.size() == 0 ) {
stmt.executeUpdate("insert into cvt(id, name, parentid, type) values(null, '"
+ name + "'," + 0 + ", '" + type + "');");
} else if (tbList.get(0).getFatherId().equals("0")) {
continue;
}
} else {
ArrayList<TestBean> tbList1 = new ArrayList<TestBean>();
tbList1 = dbUtil.selNode(name, type, conn);
System.out.println("tbList1: " + tbList1.size());
int fid;
if(tbList1.size() == 0) {
fid = 0;
} else {
fid = Integer.parseInt(tbList1.get(0).getFatherId());
}
if(fid > 0) {
ArrayList<TestBean> parentList = new ArrayList<TestBean>();
parentList = dbUtil.selParentNode(fid, conn);
String currentParent = strs[i + 1];
if(parentList.get(0).getName() == currentParent.substring(currentParent.indexOf("=") + 1) && parentList.get(0).getType() == currentParent.substring(0,currentParent.indexOf("="))) {
continue;
} else {
stmt.executeUpdate("insert into cvt(id, name, parentid, type) values(null, '"
+ currentParent.substring(currentParent.indexOf("=") + 1) + "'," + parentList.get(0).getId() + ", '" + currentParent.substring(0,currentParent.indexOf("=")) + "');");
}
} else {
stmt.executeUpdate("insert into cvt(id, name, parentid, type) values(null, '"
+ name + "'," + 0 + ", '" + type + "');");
}
} conn.commit();
}
}
} catch (Exception e) {
// 异常处理
e.printStackTrace();
} finally {
// 关闭连接,释放资源
dbUtil.closeConnection(conn);
}
}

public void comparePID(int pid, int prePid) {
DBUtil dbUtil = DBUtil.getInstance("127.0.0.1", "test", "3306", "root","root");
try {
if(pid == prePid) {
comparePID(pid, prePid);
} else {

}
} catch(Exception e) {
e.printStackTrace();
}
}
}
但是我实现的效果是这样的:
---目前我发现的问题是,我写的程序没判断到某一节点的爷爷是谁。。觉得还是面向对象比较靠谱一点。诚邀各路大神来帮帮忙修改一下,或者写一个面向对象的让我学习学习。。小弟在此感谢感谢
数据库java面向对象树状结构

解决方案 »

  1.   

    别的就不写了,写几个对象类吧。
    集团类
    Class group{
    public String name;//集团名
    //其他属性
    public List<company> list;//或者这里用Map也行
    //提供构造器,get,set方法
    }公司类
    Class company{
    public String name;//公司名
    //其他属性
    public List<department> list;//或者这里用Map也行
    //提供构造器,get,set方法
    }
    这样构建对象就行了。