关于java解析字符串,然后存入数据库的一个问题,求教解答。 本帖最后由 woaitengyu01 于 2013-07-02 19:31:57 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我自己实现了一个基本功能的(存在问题:1、没与数据库交互,如果重复插入就会出现重复数据 2、ID的分配有点问题)此次我试用了3个类来实现:TestBean.java, ReadTxt.java, DBUtil.java//TestBean.javapackage com.toony.bean;public class TestBean { private String id; private String name; private String fatherId; private String type; public TestBean() { this.id = id; this.name = name; this.fatherId = fatherId; this.type = type; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFatherId() { return fatherId; } public void setFatherId(String fatherId) { this.fatherId = fatherId; } public String getType() { return type; } public void setType(String type) { this.type = type; } }//DBUtil.javapackage com.toony.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;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 void insData(TestBean tb, Connection conn) { try { PreparedStatement pstmt = conn.prepareStatement("insert into cvte values(?,?,?,?)"); pstmt.setString(1, tb.getId()); pstmt.setString(2, tb.getName()); pstmt.setString(3, tb.getFatherId()); pstmt.setString(4, tb.getType()); pstmt.executeUpdate(); } catch(Exception e) { e.printStackTrace(); } } }//ReadTxt.javaimport java.io.*;import java.sql.Connection;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import com.toony.bean.TestBean;import com.toony.util.DBUtil;public class ReadTxt { public static void main(String[] args) { try { File f = new File("E:\\data.txt"); BufferedReader br = new BufferedReader(new FileReader(f)); String readLine; HashMap<String, ArrayList<TestBean>> m = new HashMap<String, ArrayList<TestBean>>(); ArrayList<ArrayList<TestBean>> tbList = new ArrayList<ArrayList<TestBean>>(); int fid = 0; DBUtil dbUtil = DBUtil.getInstance("127.0.0.1", "test", "3306", "root", "root"); Connection conn = dbUtil.getConnection(); while((readLine = br.readLine()) != null) { if(readLine.indexOf("/") != -1) { int urlLastIndex = readLine.lastIndexOf("/"); String tempStr = readLine.substring(0, urlLastIndex+1); readLine = readLine.substring(tempStr.length(), readLine.length()); String[] arrStr = readLine.split(","); String[] arrStr1 = new String[arrStr.length]; int index = 0; for(int i = arrStr.length - 1; i >= 0; i--) { String temp = arrStr[i].substring(arrStr[i].indexOf("=") + 1, arrStr[i].length()); arrStr1[index++] = temp; } ArrayList<TestBean> list = new ArrayList<TestBean>(); for(int i = 0; i < arrStr1.length; i++) { System.out.println(arrStr1[i]); TestBean tb = new TestBean(); tb.setId(i == 0 ? "0" : (++fid) + "" ); tb.setName(arrStr1[i]); tb.setFatherId(i == 0 ? "" : list.get(i-1).getId()); tb.setType(i==0 || i == 1 ? "公司" : i == arrStr1.length - 1 ? "人员" : "部门"); dbUtil.insData(tb, conn); list.add(tb); } tbList.add(list); } else { //System.out.println("----------"); } } for(int i = 0; i < tbList.size(); i++) { ArrayList list1 = tbList.get(i); for(int j = 0; j < list1.size(); j++) { TestBean tb = (TestBean)list1.get(j); System.out.println(tb.getId() + " : " + tb.getName() + " : " +tb.getType() + " :" + tb.getFatherId()); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("文件不存在,请确认您的文件是否存在,或者是路径是否错误"); e.printStackTrace(); } catch (IOException e) { System.out.println("read or write file happen error,please check.!"); e.printStackTrace(); } } } 这个还是要花时间的。建议LZ自己做,出现什么问题,可以再针对某个问题提问,这样对这个问题熟悉的人,也能给你一些帮助。1、没与数据库交互,如果重复插入就会出现重复数据--那可以先查询,判断不重复再插入啊2、ID的分配有点问题--什么问题要讲清楚的 简单浏览了一下lz的代码,群主说的没有与数据库交互,看代码是实现了数据库的链接,至于重复数据的出现楼上的是一种解决方法,如果你的id不是设置的主键你也可以把四个键同时设为主键。id分配是群主手工设置的,建议把id设置为自增列的,父id可以通过查询找到,这样更保险些。群主记录的字段分的很巧妙,是每条记录的规则都是 人员 部门 公司 公司 这样的吗? 还有一个问题是如果有1000条人员记录可以采用批处理提高效率。 很感谢您的回复哈。1、由于这个给定的字符串中没有规定哪个是主键,所以我是想数据库中将name值全部查询出来,然后再去比较即将导入的数据中的name值。(但是我再想,遇到名字一样的我该肿么办。)2、其实我是我感觉我的ID分配有点问题,但是我也没想到是什么问题。。压力山大了。。 1、由于这个给定的字符串中没有规定哪个是主键,所以我是想数据库中将name值全部查询出来,然后再去比较即将导入的数据中的name值。(但是我再想,遇到名字一样的我该肿么办。--name确实不应当做主见,重名太多了。可以考虑搞个联合主键,或者另外设计一个ID什么的作为主键2、其实我是我感觉我的ID分配有点问题,但是我也没想到是什么问题。。压力山大了。。--ID建议是输入的,比如身份证号码什么的,这样比较有意义感觉LZ的问题不是编码,是思路,呵呵~ 现在老师又说名字是唯一的了。omg,不过我的思路确实好像是有点不太正确,又得重新想想咯。郁闷... 现在老师又说名字是唯一的了。omg,不过我的思路确实好像是有点不太正确,又得重新想想咯。郁闷...嗯,思路很重要既然名字唯一,可以作为主键的,系统可以支持重入(即,后面输入的覆盖前面的内容) 字符串:LDAP://gz.cvte.cn/CN=张三,OU=研发中心,OU=事业部,OU=AAA有限公司,OU=C集团LDAP://gz.cvte.cn/CN=tom,OU=销售部,OU=事业部,OU=AAA有限公司,OU=C集团......表结构:(可以修改,反正能体现到树状结构就好了)idname:名称fatherId: 父Idtype:类型(人员,公司,部门)问题:解析上述字符串,然后按照下列树状形式存入数据库,数据库不限。PS:【上述字符串中 人员的名字, 集团的名字, xxx有限公司的名字都是唯一的】 C集团 (公司) |-AA有限公司 (公司) | |-电源事业部门 (部门) | |-研发中心 (部门) | |-张三 (人员) | |-BBB公司 (公司) |-研发中心 (部门) |-李四 (人员)跪求各路大神解答,我弄了两天都没弄出来。。哎哎呀呀呀呀呀。。 求教一个关于Java虚拟机版本的小白问题 java打包问题 在网上买了一个JAVA空间,怎么在这个空间里面配置两个项目? 请教一个建表的问题 不太难的问题,我有思路,但没做出来.50分啊~ 如何调用add()函数 有没有办法模拟鼠标左键点击事件?? 关于applet里更新图片的问题 关于jtable的显示 如何监听这个事件?给点源代码最好! 相同项目在不同环境下的JAR包冲突,是为什么? 启动quartz后,前几次任务的启动时间混乱
(存在问题:1、没与数据库交互,如果重复插入就会出现重复数据
2、ID的分配有点问题
)此次我试用了3个类来实现:
TestBean.java, ReadTxt.java, DBUtil.java
//TestBean.java
package com.toony.bean;public class TestBean {
private String id;
private String name;
private String fatherId;
private String type;
public TestBean() {
this.id = id;
this.name = name;
this.fatherId = fatherId;
this.type = type;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getFatherId() {
return fatherId;
} public void setFatherId(String fatherId) {
this.fatherId = fatherId;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
}
}//DBUtil.java
package com.toony.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;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 void insData(TestBean tb, Connection conn) {
try {
PreparedStatement pstmt = conn.prepareStatement("insert into cvte values(?,?,?,?)");
pstmt.setString(1, tb.getId());
pstmt.setString(2, tb.getName());
pstmt.setString(3, tb.getFatherId());
pstmt.setString(4, tb.getType());
pstmt.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
}
}
}
//ReadTxt.java
import java.io.*;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;import com.toony.bean.TestBean;
import com.toony.util.DBUtil;
public class ReadTxt {
public static void main(String[] args) {
try {
File f = new File("E:\\data.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
String readLine;
HashMap<String, ArrayList<TestBean>> m = new HashMap<String, ArrayList<TestBean>>();
ArrayList<ArrayList<TestBean>> tbList = new ArrayList<ArrayList<TestBean>>();
int fid = 0;
DBUtil dbUtil = DBUtil.getInstance("127.0.0.1", "test", "3306", "root", "root");
Connection conn = dbUtil.getConnection();
while((readLine = br.readLine()) != null) {
if(readLine.indexOf("/") != -1) {
int urlLastIndex = readLine.lastIndexOf("/");
String tempStr = readLine.substring(0, urlLastIndex+1);
readLine = readLine.substring(tempStr.length(), readLine.length());
String[] arrStr = readLine.split(",");
String[] arrStr1 = new String[arrStr.length];
int index = 0;
for(int i = arrStr.length - 1; i >= 0; i--) {
String temp = arrStr[i].substring(arrStr[i].indexOf("=") + 1, arrStr[i].length());
arrStr1[index++] = temp;
}
ArrayList<TestBean> list = new ArrayList<TestBean>();
for(int i = 0; i < arrStr1.length; i++) {
System.out.println(arrStr1[i]);
TestBean tb = new TestBean();
tb.setId(i == 0 ? "0" : (++fid) + "" );
tb.setName(arrStr1[i]);
tb.setFatherId(i == 0 ? "" : list.get(i-1).getId());
tb.setType(i==0 || i == 1 ? "公司" : i == arrStr1.length - 1 ? "人员" : "部门");
dbUtil.insData(tb, conn);
list.add(tb);
}
tbList.add(list);
} else {
//System.out.println("----------");
}
}
for(int i = 0; i < tbList.size(); i++) {
ArrayList list1 = tbList.get(i);
for(int j = 0; j < list1.size(); j++) {
TestBean tb = (TestBean)list1.get(j);
System.out.println(tb.getId() + " : " + tb.getName() + " : " +tb.getType() + " :" + tb.getFatherId());
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件不存在,请确认您的文件是否存在,或者是路径是否错误");
e.printStackTrace();
} catch (IOException e) {
System.out.println("read or write file happen error,please check.!");
e.printStackTrace();
}
}
}
--那可以先查询,判断不重复再插入啊
2、ID的分配有点问题
--什么问题要讲清楚的
很感谢您的回复哈。1、由于这个给定的字符串中没有规定哪个是主键,所以我是想数据库中将name值全部查询出来,然后再去比较即将导入的数据中的name值。(但是我再想,遇到名字一样的我该肿么办。)2、其实我是我感觉我的ID分配有点问题,但是我也没想到是什么问题。。压力山大了。。
1、由于这个给定的字符串中没有规定哪个是主键,所以我是想数据库中将name值全部查询出来,然后再去比较即将导入的数据中的name值。(但是我再想,遇到名字一样的我该肿么办。
--name确实不应当做主见,重名太多了。可以考虑搞个联合主键,或者另外设计一个ID什么的作为主键2、其实我是我感觉我的ID分配有点问题,但是我也没想到是什么问题。。压力山大了。。
--ID建议是输入的,比如身份证号码什么的,这样比较有意义感觉LZ的问题不是编码,是思路,呵呵~
现在老师又说名字是唯一的了。omg,不过我的思路确实好像是有点不太正确,又得重新想想咯。郁闷...
现在老师又说名字是唯一的了。omg,不过我的思路确实好像是有点不太正确,又得重新想想咯。郁闷...
嗯,思路很重要
既然名字唯一,可以作为主键的,系统可以支持重入(即,后面输入的覆盖前面的内容)
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公司 (公司)
|-研发中心 (部门)
|-李四 (人员)跪求各路大神解答,我弄了两天都没弄出来。。哎哎呀呀呀呀呀。。