The xml file is read to the database, and the xml file is read to the database.

Source: Internet
Author: User
Tags domian

The xml file is read to the database, and the xml file is read to the database.

Read xml files to the database

 

Step 1: import the package

C3p0, dom4j, jaxen, MySQL-connector

Step 2: xml file, config File

Step 3: javabean

Step 4: tool class of c3p0

Step 5: Read the xpath in the xml file SAXReader

First, you need to add aliases to the map set to traverse the files read,

List <javabean>

Step 6: list <javabean> gives the c3p0 database connection class

 

 

 

Step 1: import the package

C3p0, dom4j, jaxen, MySQL-connector

 

Step 2: xml file, config File

Xml file. The config file is implemented as needed

Xsd Constraints

<? 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>

Step 3: javabean

Perform

Step 4: tool class of 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;

}

}

}

 

Step 5: Read the xpath in the xml file SAXReader

First, you need to add aliases to the map set to traverse the files read,

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. extends entexception;

Import org. dom4j. Element;

Import org. dom4j. io. SAXReader;

Import org. junit. Test;

 

Import com. itwjx. entity. XMLDomain;

Import com. itwjx. util. C3P0Util;

 

/**

* Database name demo

* Table Name userDomain

* Field: id int

* Name varchar

* Birthday date

* Holobby char

* @ Author WBH

*

*/

Public class WrokXmlAns {

@ Test

Public void readXMLtoDB (){

Try {

// Read XML file data

List <XMLDomain> domains = readXML ("src/aaa. xml ");

// Save the data to the database

SaveXMLDateToDB (domains );

} Catch (Exception e ){

E. printStackTrace ();

}

}

 

 

Private List <XMLDomain> readXML (String path) throws into entexception, 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 holobby = element. element ("holobby"). getText ();

 

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd ");

Date date = df. parse (birthday );

 

XMLDomain domian = new XMLDomain (

Integer. parseInt (id), name, date, holobby );

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. getholobby ());

 

Ps. addBatch ();

}

 

Ps.exe cuteBatch ();

} Catch (Exception e ){

E. printStackTrace ();

} Finally {

C3P0Util. release (null, ps, conn );

}

}

}

 

Step 6: list <javabean> gives the c3p0 database connection class

 

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. getholobby ());

 

Ps. addBatch ();

}

 

Ps.exe cuteBatch ();

} Catch (Exception e ){

E. printStackTrace ();

} Finally {

C3P0Util. release (null, ps, conn );

}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.