Obtain mysql content, generate an xml file, and send it as webservice

Source: Internet
Author: User

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

In fact, three knowledge points are integrated, including how to access mysql, how to generate xml files, and how to use webservice development.

Okay, let's not talk about it. Stick the Code directly.

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 a root node list; Element root = new Element ("xyt "); // Add the root node to the Document; Document Doc = new Document (root); Connection conn = null; Statement stmt = null; // here The for loop can be replaced with the result set operation for Traversing database tables. 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 name of your database try {conn = DriverManager. getConnection (url_connect);} catch (SQLException e0) {// TODO Auto-generated catch 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 rs1_stmt.exe cuteQuery (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 a node user; Element elements = new Element ("news"); // Add a property id to the user node; elements. setAttribute ("id", page_id); // Add a subnode to the user node and assign a value; // Replace "name" in new Element ("name") with the corresponding fields in the table, in setText ("xuehui"), replace "xuehui 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); // Add a user subnode to the parent node list; 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 );}}




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.