Get MySQL content, generate XML files, and in addition, for WebService send

Source: Internet
Author: User

<xyt>          <news id>                <url></url>                <title></title>
          </news></xyt>

The fact is that three points of knowledge are integrated together. Includes how to access MySQL. How to generate an XML file and how to use it for WebService development

All right. Words don't say much. Direct Stick Code

Package Com.service;import Java.io.fileoutputstream;import java.io.ioexception;import java.sql.*;import org.jdom.*; Import Org.jdom.output.xmloutputter;public class Newsimpl implements Newsinterface{public String getnews (int startpage    , int endpage) {//Create root node list;     element root = new Element ("Xyt");       The root node is added to the document; doc = new document (root);    Connection conn = null;   Statement stmt = null;    Here the For loop can be replaced by a result set operation that traverses the database table; try {class.forname ("Org.gjt.mm.mysql.Driver"). newinstance (); String url_connect = "jdbc:mysql://localhost/nxu_life?user=root&password=12345&useunicode=true&        characterencoding=gb2312 "; First for your database name try {conn = drivermanager.getconnection (url_connect);} catch (SQLException E0) {//TODO auto-generated Cat        CH Blocke0.printstacktrace ();} try {stmt = conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);} catch (SQLException        E00) {//TODO auto-generated catch Blocke00.printstacktrace ();} StRing sql= "Select Page_id,title,url,publishtime,page,department from News where page_id>=" +startpage+ "and page_id        <= "+endpage;   try {ResultSet rs=stmt.executequery (SQL), while (Rs.next ()) {String page_id=rs.getstring ("page_id");   String title=rs.getstring ("title");   String url=rs.getstring ("url");   String publishtime=rs.getstring ("Publishtime");   String page=rs.getstring ("page");           String department=rs.getstring ("department");           Create node user;           Element elements = new Element ("News");           Add the attribute ID to the user node;                     Elements.setattribute ("id", page_id); Add a child node to the user node and assign a value;//the "name" in the new Element ("name") is replaced with the corresponding field in the table.           SetText ("Xuehui") in "Xuehui replaced with the record value in the table, Elements.addcontent (New Element (" url "). SetText (URL));           Elements.addcontent (New Element ("title"). SetText (title));           Elements.addcontent (New Element ("Publishtime"). SetText (Publishtime)); Elements.addcontent (New Element ("page"). SetText (page));           Elements.addcontent (New Element ("department"). SetText (department));           The parent node list is added to the user child node;        Root.addcontent (elements); }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (Instantiationexception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} catch ( Illegalaccessexception E2) {//TODO auto-generated catch Blocke2.printstacktrace ();} catch (ClassNotFoundException E3) {        TODO auto-generated catch Blocke3.printstacktrace ();}    Xmloutputter xmlout = new Xmloutputter ();    String xmlstring =xmlout.outputstring (DOC);    return xmlstring;    System.out.println (xmlstring); }}




Get MySQL content, generate XML files, and in addition, for WebService send

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.