62,621
社区成员
发帖
与我相关
我的任务
分享
//DBUtil.java --用于 连接数据库 和 查询数据库中的数据 的一个类
package com.toony.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
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 int selParentId(String parentName, String parentType, Connection conn) {
String sql = "select id from cvt where name = '" + parentName + "' and type = '" + parentType + "'";
int parentId = 0;
ResultSet rs = null;
Statement stmt = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
parentId = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {rs.close(); rs = null;}
if(stmt != null) {stmt.close(); stmt = null;}
} catch (Exception e) {
e.printStackTrace();
}
}
return parentId;
}
public ArrayList<TestBean> selParentNode(int pid, Connection conn) {
String strSql = "select id, name, parentId, type from cvt where id = '" + pid + "'";
Statement stmt = null;
ResultSet rs = null;
ArrayList<TestBean> tempList = new ArrayList<TestBean>();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
if(rs.next()) {
TestBean tb = new TestBean();
tb.setId(rs.getInt("id")+"");
tb.setName(rs.getString("name"));
tb.setFatherId(rs.getString("parentId"));
tb.setType(rs.getString("type"));
tempList.add(tb);
}
} catch(Exception e) {
e.printStackTrace();
}
return tempList;
}
public ArrayList<TestBean> selNode(String name, String type, Connection conn) {
String strSql = "select id, name, parentId, type from cvt where name = '" + name + "' and type = '" + type + "'";
Statement stmt = null;
ResultSet rs = null;
ArrayList<TestBean> tempList = new ArrayList<TestBean>();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
if(rs.next()) {
TestBean tb = new TestBean();
tb.setId(rs.getInt("id")+"");
tb.setName(rs.getString("name"));
tb.setFatherId(rs.getString("parentId"));
tb.setType(rs.getString("type"));
tempList.add(tb);
}
} catch(Exception e) {
e.printStackTrace();
}
return tempList;
}
public boolean nodeIsExist(String name, String type, Connection conn) {
String sql = "select count(1) from cvt where name = '" + name + "' and type = '" + type + "'";
Statement stmt = null;
int counter = 0;
boolean isExist = false;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()) {
counter = rs.getInt(1);
}
if(counter > 0) {
isExist = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt != null) {stmt.close(); stmt = null; }
} catch (Exception e) { e.printStackTrace(); }
}
return isExist;
}
public void closeConnection(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
//Test.java --main类
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.toony.bean.TestBean;
import com.toony.util.DBUtil;
public class Test {
public static void main(String[] args) {
List<String> list = readFile("E:\\data.txt");
for (String s : list) {
inDB(s);
}
}
public static List<String> readFile(String path) {
File file = new File(path); // 文件路径
List<String> lists = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while (null != (line = br.readLine())) {
lists.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lists;
}
public static void inDB(String str) {
Connection conn = null;
Statement stmt = null;
DBUtil dbUtil = DBUtil.getInstance("127.0.0.1", "test", "3306", "root","root");
conn = dbUtil.getConnection();
// String readLine;
int parentId = 0;
try {
conn.setAutoCommit(false);
stmt = conn.createStatement();
int urlLastIndex = str.lastIndexOf("/");
String tempStr = str.substring(0, urlLastIndex + 1);
str = str.substring(tempStr.length(), str.length());
String[] strs = str.split(",");
ArrayList<TestBean> tbList;
for (int i = strs.length - 1; i > -1; i--) {
// System.out.println(strs[i]);
String str1 = strs[i];
String name = str1.substring(str1.indexOf("=") + 1);
String type = str1.substring(0, str1.indexOf("="));
if (dbUtil.nodeIsExist(name, type, conn)) {
continue;
} else {
if(i == strs.length - 1) {
tbList = new ArrayList<TestBean>();
tbList = dbUtil.selNode(name, type, conn);
System.out.println(tbList.size());
if(tbList == null || tbList.size() == 0 ) {
stmt.executeUpdate("insert into cvt(id, name, parentid, type) values(null, '"
+ name + "'," + 0 + ", '" + type + "');");
} else if (tbList.get(0).getFatherId().equals("0")) {
continue;
}
} else {
ArrayList<TestBean> tbList1 = new ArrayList<TestBean>();
tbList1 = dbUtil.selNode(name, type, conn);
System.out.println("tbList1: " + tbList1.size());
int fid;
if(tbList1.size() == 0) {
fid = 0;
} else {
fid = Integer.parseInt(tbList1.get(0).getFatherId());
}
if(fid > 0) {
ArrayList<TestBean> parentList = new ArrayList<TestBean>();
parentList = dbUtil.selParentNode(fid, conn);
String currentParent = strs[i + 1];
if(parentList.get(0).getName() == currentParent.substring(currentParent.indexOf("=") + 1) && parentList.get(0).getType() == currentParent.substring(0,currentParent.indexOf("="))) {
continue;
} else {
stmt.executeUpdate("insert into cvt(id, name, parentid, type) values(null, '"
+ currentParent.substring(currentParent.indexOf("=") + 1) + "'," + parentList.get(0).getId() + ", '" + currentParent.substring(0,currentParent.indexOf("=")) + "');");
}
} else {
stmt.executeUpdate("insert into cvt(id, name, parentid, type) values(null, '"
+ name + "'," + 0 + ", '" + type + "');");
}
}
conn.commit();
}
}
} catch (Exception e) {
// 异常处理
e.printStackTrace();
} finally {
// 关闭连接,释放资源
dbUtil.closeConnection(conn);
}
}
public void comparePID(int pid, int prePid) {
DBUtil dbUtil = DBUtil.getInstance("127.0.0.1", "test", "3306", "root","root");
try {
if(pid == prePid) {
comparePID(pid, prePid);
} else {
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
