woaitengyu01 2013-07-04 09:28
浏览 430
已采纳

java解析字符串,按照树形结构存入数据库--这个问题纠结了我好久,各路大神求帮忙

[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]

请求大神解答,我弄了两天都没弄出来。。哎哎呀呀呀呀呀。。

  • 写回答

6条回答

  • QiangGe2Dai 2013-07-05 11:14
    关注

    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();
    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题