字符串:
LDAP://gz.cvte.cn/CN=张三,OU=研发中心,OU=事业部,OU=AAA有限公司,OU=C集团
LDAP://gz.cvte.cn/CN=tom,OU=销售部,OU=事业部,OU=AAA有限公司,OU=C集团
......表结构:(可以修改,反正能体现到树状结构就好了)
id
name:名称
fatherId: 父Id
type:类型(人员,公司,部门)问题:解析上述字符串,然后按照下列树状形式存入数据库,数据库不限。
PS:【上述字符串中 人员的名字, 集团的名字, xxx有限公司的名字都是唯一的】C集团 (公司)
|-AA有限公司 (公司)
| |-电源事业部门 (部门)
| |-研发中心 (部门)
| |-张三 (人员)
|
|-BBB公司 (公司)
|-研发中心 (部门)
|-李四 (人员)
跪求大神解答,我弄了两天都没弄出来。。哎哎呀呀呀呀呀。。
万分感谢感谢啊!
java数据库树状结构解析
LDAP://gz.cvte.cn/CN=张三,OU=研发中心,OU=事业部,OU=AAA有限公司,OU=C集团
LDAP://gz.cvte.cn/CN=tom,OU=销售部,OU=事业部,OU=AAA有限公司,OU=C集团
......表结构:(可以修改,反正能体现到树状结构就好了)
id
name:名称
fatherId: 父Id
type:类型(人员,公司,部门)问题:解析上述字符串,然后按照下列树状形式存入数据库,数据库不限。
PS:【上述字符串中 人员的名字, 集团的名字, xxx有限公司的名字都是唯一的】C集团 (公司)
|-AA有限公司 (公司)
| |-电源事业部门 (部门)
| |-研发中心 (部门)
| |-张三 (人员)
|
|-BBB公司 (公司)
|-研发中心 (部门)
|-李四 (人员)
跪求大神解答,我弄了两天都没弄出来。。哎哎呀呀呀呀呀。。
万分感谢感谢啊!
java数据库树状结构解析
解决方案 »
- 返回自 1970年1月1日00:00:00 GMT以来对象表示的毫秒数??
- 如何把Oracle数据库中的blob转成pdf格式
- 小弟求助一道JAVA问题
- 写一个java程序,实现对一个二维数组按指定的列集进行排序?要求实现类似sql中order by的功能,移动时,整行移动,不能打乱整行顺序。
- 如何在XML Doucument返回指定的Element,如果找不到,返回null
- 帮我看看,有办法没有?
- java报错 <identifier>expected什么意思啊?
- 初学Java,写了个小applet不知哪错了,帮忙改一下!!!
- 请问那位有画图表时确定坐标轴刻度的算法谢谢?
- 有没有jbuilder下载 ?
- 请问这个正则表达式怎么写?
- 关于单粒模式的疑问,我咋觉得网上都是人云亦云呢?
LDAP://gz.cvte.cn/CN=张三,OU=研发中心,OU=事业部,OU=AAA有限公司,OU=C集团
先,分割
然后判断级别,是集团还是公司还是部分等等。。
判断好了之后放入对象类。
//DB方法
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 test where parentId = '" + 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 boolean nodeIsExist(String name, String type, Connection conn) {
String sql = "select count(1) from test 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 < 1 && counter != -1) {
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();
}
}
}//main 方法
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", "mysql", "3306", "root", "root");
conn = dbUtil.getConnection();
//String readLine;
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(",");
for (int i = strs.length - 1; i >= 0; i--) {
//System.out.println(strs[i]);
String str1 = strs[i];
String name = str1.substring(str1.indexOf("=") + 1);
String type = str1.substring(0, str1.indexOf("="));
int parentId = 0;
if(dbUtil.nodeIsExist(name, type, conn)) {
continue;
}
if(i < strs.length - 1) {
String parent = strs[i + 1];
String parentName = parent.substring(parent.indexOf("=") + 1);
System.out.println(parentName);
String parentType = parent.substring(0, parent.indexOf("="));
parentId = dbUtil.selParentId(parentName, parentType, conn);
}
if(parentId > 0) {
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ name
+ "'," + parentId + ", '"
+ type + "');");
} else {
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ name
+ "'," + 0 + ", '"
+ type + "');");
}
conn.commit();
}
} catch (Exception e) {
// 异常处理
e.printStackTrace();
} finally {
// 关闭连接,释放资源
dbUtil.closeConnection(conn);
}
}现在我遇到几个问题:
1、解析了字符串,但是没按照树状结构存入数据库(可能我逻辑哪里错了,大家能帮我看看我的代码给点建议么);
2、重复运行程序,居然没检查数据是否重复,还继续添加在原有数据后面。(但是我有写了判断数据库与现在存入的字符串数据是否重复啊。)我最终要的结构是:
我弄成这样了
请大家帮帮忙看一下小弟在此感谢了。
谢谢你的回复,我改了,但是结果还是有点问题,OH。my..god..
变成这样了。,,搞的脑袋有点混乱了,能指点一下我哪里逻辑有问题么?,谢谢你哈
if(counter > 0) {
isExist = true;
}但是,又出现一个问题,就是
---少了一个研发部门,因为有一个研发部门是在AAA公司下面的部门子部门的,另外一个是BB公司的研发部门,但是现在合并了。。我那些逻辑错了么?请大家指明一下哈。谢谢大家
continue;
}
这个地方进行了判断,如果名字和类型相同就不执行插入操作,而研发中心的名字和类型是完全一样的,所以现在只有一个。你可以判断是否存在数据库,判断之后查询数据库中已存在的那条数据的parentId,然后查询当前要插入数据的parentId,如果parentId不相同就执行插入操作。