注:前提是把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
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 {
File doc = new File("E:/assets");
if (doc.exists() && doc.isDirectory()) {
File[] flist = doc.listFiles(new filefilter());
if (flist.length > 0) {
list = new ArrayList();
for (int i = 0; i < flist.length; i++) {
list.add(flist[i]);
}
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return list;
}
private class filefilter implements FilenameFilter {
@Override
public boolean accept(File arg0, String arg1) {
// TODO Auto-generated method stub
return arg1.endsWith("xml");
}
}
}