xml檔案讀取到資料庫,xml檔案讀取資料庫

來源:互聯網
上載者:User

xml檔案讀取到資料庫,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的工具類

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

}

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.