62,615
社区成员
发帖
与我相关
我的任务
分享
C集团 (公司)
|-AA有限公司 (公司)
| |-电源事业部门 (部门)
| |-研发中心 (部门)
| |-张三 (人员)
|
|-BBB公司 (公司)
|-研发中心 (部门)
|-李四 (人员)
C集团 (公司)
|-AA有限公司 (公司)
| |-电源事业部门 (部门)
| |-研发中心 (部门)
| |-张三 (人员)
|
|-BBB公司 (公司)
|-研发中心 (部门)
|-李四 (人员)
跪求各路大神解答,我弄了两天都没弄出来。。哎哎呀呀呀呀呀。。
//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();
}
}
}