1. 程式人生 > >【Java】XML檔案讀取到資料庫

【Java】XML檔案讀取到資料庫

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的工具類

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>

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

 *

 */

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);

}

}