xml文件怎么读取到数据库
程序员文章站
2022-05-18 13:28:51
...
xml文件读取到数据库
第一步,导包
c3p0,dom4j,jaxen,MySQL-connector
第二步 xml文件,config文件
第三步 javabean
第四步 c3p0的工具类
第五步 读取xml文件 SAXReader中的xpath的方式
首先需要map集合添加别名,遍历读取到的文件,
给了list<javabean>
第六步,list<javabean>给了c3p0的连接数据库的类
第一步,导包
c3p0,dom4j,jaxen,MySQL-connector
第二步 xml文件,config文件
xml文件,config文件根据需求具体实现
Xsd的约束
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="xiaoge" elementFormDefault="qualified"> <element name="group"> <complexType> <sequence maxOccurs="8" minOccurs="1"> <element name="person"> <complexType> <sequence> <element name="name" type="string"></element> <element name="sex" type="string"></element> <element name="age" type="string"></element> </sequence> </complexType> </element> </sequence> <attribute name="id" type="int" use="required"></attribute> </complexType> </element> </schema>
第三步 javabean
根据需求在具体做
第四步 c3p0的工具类
package com.itheima.util;
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Util { private static final ComboPooledDataSource DATASOURCE = new ComboPooledDataSource(); public static Connection getConn(){ try { return DATASOURCE.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void release(ResultSet rs, Statement stmt, Connection conn){ if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } } }
第五步 读取xml文件 SAXReader中的xpath的方式
首先需要map集合添加别名,遍历读取到的文件,
给了list<javabean>
package com.itwjx.xml;
import java.sql.Connection; import java.sql.PreparedStatement; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.junit.Test; import com.itwjx.entity.XMLDomain; import com.itwjx.util.C3P0Util;
/** * 数据库名称 demo * 表名userDomain * 字段: id int * name varchar * birthday date * hobby char * @author WBH * */ public class WrokXmlAns { @Test public void readXMLtoDB(){ try { //读取XML文件数据 List<XMLDomain> domains = readXML("src/aaa.xml"); //将数据保存到数据库 saveXMLDateToDB(domains); } catch (Exception e) { e.printStackTrace(); } } private List<XMLDomain> readXML(String path) throws DocumentException, ParseException { SAXReader read = new SAXReader(); Document document = read.read(path); Map<String, String> map = new HashMap<String, String>(); map.put("wbh", "xiaofan"); read.getDocumentFactory().setXPathNamespaceURIs(map); List<Element> nodes = document.selectNodes("//wbh:member"); List<XMLDomain> domains = new ArrayList<XMLDomain>(); for (Element element : nodes) { String id = element.attributeValue("no"); String name = element.element("name").getText(); String birthday = element.element("birthday").getText(); String hobby = element.element("hobby").getText(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date date = df.parse(birthday); XMLDomain domian = new XMLDomain( Integer.parseInt(id), name,date, hobby); domains.add(domian); } return domains; } private void saveXMLDateToDB(List<XMLDomain> domains) { // Connection conn = null; PreparedStatement ps = null; try { conn = C3P0Util.getConn(); ps = conn.prepareStatement("insert into userDomain values(?,?,?,?)"); for (XMLDomain user : domains) { ps.setInt(1, user.getId()); ps.setString(2, user.getName()); ps.setDate(3, new java.sql.Date(user.getBirthday().getTime())); ps.setString(4, user.getHobby()); ps.addBatch(); } ps.executeBatch(); } catch (Exception e) { e.printStackTrace(); } finally { C3P0Util.release(null, ps, conn); } } }
第六步,list<javabean>给了c3p0的连接数据库的类
private void saveXMLDateToDB(List<XMLDomain> domains) { // Connection conn = null; PreparedStatement ps = null; try { conn = C3P0Util.getConn(); ps = conn.prepareStatement("insert into userDomain values(?,?,?,?)"); for (XMLDomain user : domains) { ps.setInt(1, user.getId()); ps.setString(2, user.getName()); ps.setDate(3, new java.sql.Date(user.getBirthday().getTime())); ps.setString(4, user.getHobby()); ps.addBatch(); } ps.executeBatch(); } catch (Exception e) { e.printStackTrace(); } finally { C3P0Util.release(null, ps, conn); } }
以上就是xml文件怎么读取到数据库的详细内容,更多请关注其它相关文章!