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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退