[color=red][b][size=large]字符串:[/size][/b][/color]
LDAP://gz.cvte.cn/CN=张三,OU=研发中心,OU=事业部,OU=AAA有限公司,OU=C集团
LDAP://gz.cvte.cn/CN=tom,OU=销售部,OU=事业部,OU=AAA有限公司,OU=C集团
......
[color=red]
[b][size=large]表结构:(可以修改,反正能体现到树状结构就好了)[/size][/b][/color]
id
name:名称
fatherId: 父Id
type:类型(人员,公司,部门)
[size=large][color=red]问题:解析上述字符串,然后按照下列树状形式存入数据库,数据库不限。
PS:【上述字符串中 人员的名字, 集团的名字, xxx有限公司的名字都是唯一的】
[/color][/size]
[code="java"] C集团 (公司)
|-AA有限公司 (公司)
| |-电源事业部门 (部门)
| |-研发中心 (部门)
| |-张三 (人员)
|
|-BBB公司 (公司)
|-研发中心 (部门)
|-李四 (人员)[/code]
请求大神解答,我弄了两天都没弄出来。。哎哎呀呀呀呀呀。。
for (int i = strs.length - 1; i > -1; i--) {
String str = strs[i];
/*
* 这里是从root向下插入,所以如果遇到节点已经存在的情况
* 执行查询select count(1) from test where name = nodeName and type = nodeType
/
if (node存在) {
continue; // 不再创建重复节点
}
/
* 这里需要判断需要创建节点的父节点
/
if (i < strs.length - 1) {
String parent = strs[i + 1];
/
* 执行查询select id from test where name = parentName and type = parentType
* 找到父ID
*/
}
if (父id > 0) {
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ str.substring(str.indexOf("=") + 1)
+ "', " + 父id + ", '"
+ str.substring(0, str.indexOf("=")) + "');");
conn.commit();
} else {
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ str.substring(str.indexOf("=") + 1)
+ "', 0, '"
+ str.substring(0, str.indexOf("=")) + "');");
conn.commit();
}
}
/* 这种方式顺序就是实现关系的依据 /
String str = "CN=张三,OU=研发中心,OU=事业部,OU=AAA有限公司,OU=C集团";
String[] strs = str.split(",");
for (int i = 0; i < strs.length; i++) {
String son = strs[i];
if (i < strs.length - 1) {
String parent = strs[i + 1];
System.out.println(son + "<--->" + parent);
/
* CN=张三<--->OU=研发中心
* OU=研发中心<--->OU=事业部
* OU=事业部<--->OU=AAA有限公司
* OU=AAA有限公司<--->OU=C集团
/
} else {
System.out.println(son + "<--->ROOT");
/
* OU=C集团<--->ROOT
*/
}
}
String connectionUrl = "jdbc:mysql://localhost:3306/数据库名称?user=用户名&password=密码";
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(connectionUrl);
conn.setAutoCommit(false);
stmt = conn.createStatement();
String[] strs = "CN=张三,OU=研发中心,OU=事业部,OU=AAA有限公司,OU=C集团".split(",");
for (int i = strs.length - 1; i > -1; i--) { // 先有父类才有子类,所以倒过来写
String str = strs[i];
if (i == strs.length - 1) { // 创建ROOT
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ str.substring(str.indexOf("=") + 1)
+ "', 0, '"
+ str.substring(0, str.indexOf("=")) + "')");
conn.commit();
} else { // 创建所有叶节点
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ str.substring(str.indexOf("=") + 1)
+ "', @@IDENTITY, '"
+ str.substring(0, str.indexOf("=")) + "');");
conn.commit();
}
}
} catch (Exception e) {
// 异常处理
} finally {
// 关闭连接,释放资源
}
File file = new File(文件路径);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
List lists = new ArrayList();
while(null != (line = br.readLine())) {
lists.add(line);
}
return lists;
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
/*
* 主要就这两个方法了
*/
public void mainMethod() {
List list = readFile(文件路径);
for (String s : list) {
inDB(s);
}
}
public List<String> readFile(String path) {
File file = new File(path); // 文件路径
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
List<String> lists = new ArrayList<String>();
while(null != (line = br.readLine())) {
lists.add(line);
}
return lists;
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public void inDB(String str) {
String connectionUrl = "jdbc:mysql://localhost:3306/数据库名称?user=用户名&password=密码";
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(connectionUrl);
conn.setAutoCommit(false);
stmt = conn.createStatement();
String[] strs = str.split(",");
for (int i = strs.length - 1; i > -1; i--) {
String str = strs[i];
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ str.substring(str.indexOf("=") + 1)
+ "', @@IDENTITY, '"
+ str.substring(0, str.indexOf("=")) + "');");
conn.commit();
}
} catch (Exception e) {
// 异常处理
} finally {
// 关闭连接,释放资源
}
}
闲来没事,就帮你写写。
先说说表结构,就用你这个结构
id:(自动增长)
name:名称
parentid:父ID
type:类型(人员,公司,部门)
使用DB------MYSQL5.0