Java read data from database generation XML saved to TreeMap

Source: Internet
Author: User

Package Data;import Java.io.stringwriter;import Java.sql.connection;import java.sql.drivermanager;import Java.sql.resultset;import Java.sql.statement;import Java.util.hashmap;import Javax.xml.parsers.DocumentBuilder; Import Javax.xml.parsers.documentbuilderfactory;import Javax.xml.transform.transformer;import Javax.xml.transform.transformerfactory;import Javax.xml.transform.dom.domsource;import Javax.xml.transform.stream.streamresult;import Org.w3c.dom.document;import Org.w3c.dom.element;public Class DataSet {Private Hashmap<string,string> TM = new hashmap<string,string> ();p ublic void init () {try{//1. Load driver Class.forName ("Com.mysql.jdbc.Driver");//2, get connectionstring URL = "Jdbc:mysql://localhost:3306/mydb"; Connection conn = drivermanager.getconnection (URL, "Neo", "Neo"),//3, creating Statementstatement stmt =conn.createstatement ( );//4, using statement to execute SqlString sql = "SELECT ID, Name, sex, age from user"; ResultSet rs = stmt.executequery (sql);D ocumentbuilderfactory dfact = DOCUMENTBUILDERFACTory.newinstance ();D Ocumentbuilder build = Dfact.newdocumentbuilder (); Transformerfactory tfact = Transformerfactory.newinstance (); while (Rs.next ()) {Document doc = build.newdocument (); Element user = doc.createelement ("user");d oc.appendchild (user); Element name = doc.createelement ("name"); Name.appendchild (Doc.createtextnode (rs.getstring (2)); User.appendchild ( name); Element sex = doc.createelement ("Sex"); Sex.appendchild (Doc.createtextnode (rs.getstring (3))); User.appendchild (Sex) ; Element age = doc.createelement (' age '); Age.appendchild (Doc.createtextnode (rs.getstring (4))); User.appendchild (age) ;           Transformer trans = Tfact.newtransformer ();  StringWriter writer = new StringWriter ();  Streamresult result = new Streamresult (writer);  Domsource Source = new Domsource (DOC); Trans.transform (source, result); String Skey = rs.getstring (1); String svalue = writer.tostring (); Tm.put (Skey, svalue);} Conn.close ();} catch (Exception e) {e.printstacktrace ();}} public string GetValue (string key) {return tm.geT (key);}} 

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.