獲得mysql內容,產生xml檔案,另外,為了webservice發送

來源:互聯網
上載者:User

標籤:


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

事實上是三個知識點綜合到一起的。包含怎樣訪問mysql。怎樣產生xml檔案,怎樣使用進行webservice開發

好了。話不多說。直接粘代碼

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) {// 建立根節點 list;    Element root = new Element("xyt");     // 根節點加入到文檔中;    Document Doc = new Document(root);       Connection conn = null;    Statement stmt = null ;   // 此處 for 迴圈可替換成 遍曆 資料庫表的結果集操作;    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為你的資料庫名 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 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");           // 建立節點 user;           Element elements = new Element("news");           // 給 user 節點加入屬性 id;           elements.setAttribute("id",page_id);                     // 給 user 節點加入子節點並賦值;           // new Element("name")中的 "name" 替換成表中對應欄位。setText("xuehui")中 "xuehui 替換成表中記錄值;           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));           // 給父節點list加入user子節點;           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);    }}




獲得mysql內容,產生xml檔案,另外,為了webservice發送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.