public static void importXml(File filepath) {
("开始导入");
try {
("");
String url = "jdbc:mysql://localhost:3306/xml?serverTimezone=Asia/Shanghai";
String user = "root";
String password = "root";
Connection connection = (url, user, password);
//打开事务
(false);
//sql执行语句
String exeSql = "";
//插入值
String insertValue = "";
//插入的列名
String insertColumName = "";
//sql操作语句
String optionSql = "";
//当前记录的id值
String rowID = "";
//ID的列名
String idName = "";
//以那个字段为唯一
int flag = 0;
SAXReader saxReader = new SAXReader();
//编码设置
("UTF-8");
// 当前XML文件的Document对象
Document document = (filepath);
//获取跟节点
Element rootElement = ();
Element scenarioList = ("scenarioList");
Element scenario = ("scenario");
Element model = ("model");
//遍历里面所有的子节点
for (Iterator rootItertor = (); (); ) {
Element tableNode = (Element) ();
//表名称
String tableNmae = ();
for (Iterator tableIter = ();();){
Element table = (Element) ();
List<Attribute> attributess = ();
for (Attribute attribute:attributess){
insertValue2+="'"+()+"',";
insertColumName2+="`"+()+"`,";
}
if (().trim()!=null && !().trim().equals("")){
flag++;
if(flag==1) {
rowID = ();
idName = ();
}
String columName = ();
String columValue = ();
//判断是否是最后一个 如果是最后一个就不加 ,
if (()){
insertValue+="'"+columValue+"',";
insertColumName+="`"+columName+"`,";
}else {
insertValue+="'"+columValue+"'";
insertColumName+="`"+columName+"`";
}
}
}
//这个是因为我的 xml文件内部含有字表 字表标签无属性值 添加的时候会过滤掉
if (idName!=null && !("") && rowID!=null && !("")) {
optionSql = "select * from `"+tableNmae+"` where `"+idName+"` = '"+rowID+"'";
PreparedStatement options = (optionSql);
// (options);
ResultSet opRS = ();
if (!()) {
//拼接添加数据的sql
exeSql = "insert into `" + tableNmae + "` (" + insertColumName + ") " + "values (" + insertValue + " ) ";
PreparedStatement preparedStatement = (exeSql);
// ("插入");
// ("preparedStatement = " + preparedStatement);
();
();
();
();
}
}
exeSql="";
insertValue="";
insertColumName="";
optionSql="";
rowID="";
idName="";
flag=0;
}
();
();
("=========导入完成");
}catch (Exception e){
();
}