import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XMLWritetoDB
{
public static void main(String[] args) throws DocumentException
{
SAXReader reader = new SAXReader();
Document document = reader.read(new File(
"D:\\workspace\\demo1.3\\src\\com\\cnwu\\xml\\todb\\demo.xml"));
Element element = document.getRootElement();
List<Element> list = element.elements();
Iterator<Element> iterator = list.iterator();
String database = element.getName();
while (iterator.hasNext())
{
Element e = iterator.next();
Iterator itor = e.elementIterator();
while (itor.hasNext())
{
Node idNode = (Node) itor.next();
String value1 = idNode.getText();
Node nameNode = (Node) itor.next();
String value2 = nameNode.getText();
insert(value1,value2);
}
}
}
public static String insert(String value1,String value2)
{
ResultSet rs = null;
ArrayList list = null;
Connection conn = null;
PreparedStatement stmt = null;
try
{
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String passwd = "tiger";
conn = DriverManager.getConnection(url,username,passwd);
stmt = conn
.prepareStatement("insert into map(id,name) values(?,?)");
stmt.setString(1,value1);
stmt.setString(2,value2);
stmt.execute();
return "insert successfully.";
}
catch (Exception e)
{
e.printStackTrace();
return e.toString();
}
}
}
//数据库表
//运行结果:
参考连接:http://hi.baidu.com/200770842223/item/21543d57d8b204c49f2667ea?qq-pf-to=pcqq.c2c