将批量xml文件 数据批量添加…

注:前提是把sqlite的java 驱动添加到项目

package com.joke.data;

import java.io.File;
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 java.util.Map;

public class mysqlite {
 
 public static void main(String[] args) throws Exception {
  fileUtils f=new fileUtils();
  mysqlite m=new mysqlite();
  ArrayList list=f.getAllFile();
  System.out.println("^_^开始!");
  for (File file : list) {
   ArrayList> al=f.getContent(file);
   m.add(al);
   System.out.println(file.getName()+"ok");
  }
  
  System.out.println("^_^结束!");
 }
 public void add(ArrayList> list) throws Exception {
  String filepath="E:/jokeDb.sqlite";
  Class.forName("org.sqlite.JDBC");
  Connection conn = DriverManager.getConnection("jdbc:sqlite:/"+filepath);
  //Statement stat = conn.createStatement();
  PreparedStatement prep = conn
    .prepareStatement("insert into joke(title,content,pid) values(?,?,?);");  
   
   for (int i = 0; i < list.size(); i++) {
    Map map=list.get(i);
    prep.setString(1, map.get("t"));
    prep.setString(2, map.get("m"));
    prep.setString(3, String.valueOf(i%4+1));
    prep.addBatch();
   }

  conn.setAutoCommit(false);
  prep.executeBatch();
  conn.setAutoCommit(true);
//  ResultSet rs = stat.executeQuery("select * from joke;");
//  while (rs.next()) {
//   System.out.println("title = " + rs.getString("title"));
//   System.out.println("content = " + rs.getString("content"));
//   System.out.println("pid = " + rs.getString("pid"));
//  }
//  rs.close();
  conn.close();
 }
}
文件处理类:

package com.joke.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class fileUtils {

 public ArrayList> getContent(File f) {
  ArrayList> list = null;
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  try {
   DocumentBuilder db = dbf.newDocumentBuilder();
   Document doc = db.parse(f);
   Element root = doc.getDocumentElement();
   NodeList jokelist = root.getElementsByTagName_r("joke");
   if (jokelist.getLength() > 0) {
    list = new ArrayList>();
    for (int i = 0; i < jokelist.getLength(); i++) {
     Node e = jokelist.item(i);
     Map m = new HashMap();
     NodeList nl = e.getChildNodes();
     for (int j = 0; j < nl.getLength(); j++) {
      Node no = nl.item(j);
      if (no.getNodeName() == "title") {
       if (no.getTextContent() != "")
        m.put("t", no.getTextContent());
      }
      if (no.getNodeName() == "text") {
       if (no.getTextContent() != "")
        m.put("m", no.getTextContent());
      }
     }
     if(m.get("m")!=null&&m.get("m")!="")
      list.add(m);
    }
   }
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return list;
 }

 public ArrayList getAllFile() {
  ArrayList list = null;
  try {
   

你可能感兴趣的:(Java)