6 回答

TA貢獻1810條經(jīng)驗 獲得超4個贊
閑來沒事,就幫你寫寫。
先說說表結(jié)構(gòu),就用你這個結(jié)構(gòu)
id:(自動增長)
name:名稱
parentid:父ID
type:類型(人員,公司,部門)
使用DB------MYSQL5.0

TA貢獻1851條經(jīng)驗 獲得超5個贊
String connectionUrl = "jdbc:mysql://localhost:3306/數(shù)據(jù)庫名稱?user=用戶名&password=密碼";
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(connectionUrl);
conn.setAutoCommit(false);
stmt = conn.createStatement();
String[] strs = "CN=張三,OU=研發(fā)中心,OU=事業(yè)部,OU=AAA有限公司,OU=C集團".split(",");
for (int i = strs.length - 1; i > -1; i--) { // 先有父類才有子類,所以倒過來寫
String str = strs[i];
if (i == strs.length - 1) { // 創(chuàng)建ROOT
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ str.substring(str.indexOf("=") + 1)
+ "', 0, '"
+ str.substring(0, str.indexOf("=")) + "')");
conn.commit();
} else { // 創(chuàng)建所有葉節(jié)點
stmt.executeUpdate("insert into test(id, name, parentid, type) values(null, '"
+ str.substring(str.indexOf("=") + 1)
+ "', @@IDENTITY, '"
+ str.substring(0, str.indexOf("=")) + "');");
conn.commit();
}
}
} catch (Exception e) {
// 異常處理
} finally {
// 關(guān)閉連接,釋放資源
}

TA貢獻1868條經(jīng)驗 獲得超4個贊
File file = new File(文件路徑);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
List lists = new ArrayList();
while(null != (line = br.readLine())) {
lists.add(line);
}
return lists;
} catch (FileNotFoundException e) {
} catch (IOException e) { }
添加回答
舉報