6 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
閑來(lái)沒(méi)事,就幫你寫(xiě)寫(xiě)。
先說(shuō)說(shuō)表結(jié)構(gòu),就用你這個(gè)結(jié)構(gòu)
id:(自動(dòng)增長(zhǎng))
name:名稱(chēng)
parentid:父ID
type:類(lèi)型(人員,公司,部門(mén))
使用DB------MYSQL5.0

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊
String connectionUrl = "jdbc:mysql://localhost:3306/數(shù)據(jù)庫(kù)名稱(chēng)?user=用戶(hù)名&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集團(tuán)".split(",");
for (int i = strs.length - 1; i > -1; i--) { // 先有父類(lèi)才有子類(lèi),所以倒過(guò)來(lái)寫(xiě)
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é)點(diǎn)
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貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
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) { }
添加回答
舉報(bào)