Jdom讀取資料庫並產生xml檔案樣本

來源:互聯網
上載者:User

Jdom讀取資料庫並產生xml檔案樣本

   1.WriteXml.java:

 代碼如下  

package cn.gov.csrc.xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import cn.gov.csrc.jdbc.DatabaseConnection;
/**
 * @function 使用Jdom查詢資料庫把資料寫入xml檔案中
 * @author admin
 * 
 */
public class WriteXml {
 
 public static void WriterFileToXml(){
  //建立一個xml檔案
  File file = new File("D://user.xml");
  //建立資料庫連接
  Connection conn = DatabaseConnection.getConn();
  
  String sql = "select * from user";
  
  try {
   Statement st = conn.createStatement();//建立預先處理對象
   ResultSet rs = st.executeQuery(sql);//獲得結果集
   Element root = new Element("users");//建立根項目
   while (rs.next()) {
    Element user = new Element("user");//建立子項目
    root.addContent(user);//添加子項目到根節點
    Element id = new Element("ID");//建立葉子節點
    id.setText(rs.getString("ID"));//給葉子節點賦值
    user.addContent(id);//添加葉子節點到父節點
    Element ename = new Element("USERNAME");
    ename.setText(rs.getString("USERNAME"));//給葉子節點賦值
    user.addContent(ename);  
    Element password = new Element("PASSWORD");//建立葉子節點
    password.setText(rs.getString("PASSWORD"));//給葉子節點賦值
    user.addContent(password);//添加葉子節點到父節點
    Element status = new Element("STATUS");
    status.setText(rs.getString("STATUS"));//給葉子節點賦值
    user.addContent(status);  
    Element descn = new Element("DESCN");
    descn.setText(rs.getString("DESCN"));//給葉子節點賦值
    user.addContent(descn);  
   }
   Document doc = new Document();//建立文字物件
   doc.addContent(root);//添加樹倒文本中
   Format format= Format.getCompactFormat();
   format.setIndent(" ");
   XMLOutputter out = new XMLOutputter(format);//建立輸出資料流
   FileWriter fw = new FileWriter(file);//寫資料
   out.output(doc, fw);//輸出到xml檔案中
   fw.close();//關閉寫入流
  } catch (SQLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) {
  //把資料庫的資料寫入xml檔案中
  WriteXml.WriterFileToXml();
 }
}

  2.串連資料庫java類:DatabaseConnection.java:

 代碼如下 複製代碼

package cn.gov.csrc.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
 * 串連資料庫
 * 
 * @author admin
 * 
 */
public class DatabaseConnection {

 private static final String driverName = "com.mysql.jdbc.Driver";
 private static final String url = "jdbc:mysql://localhost/springdb?autoReconnect=true&useUnicode=true&characterEncoding=UTF8";
 private static final String username = "root";
 private static final String password = "root";

 static {
  try {
   Class.forName(driverName);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }

 public static Connection getConn() {
  Connection conn = null;
  try {
   conn = DriverManager.getConnection(url, username, password);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return conn;
 }

 public static void main(String[] args) {
  Connection conn = getConn();
  System.out.println(conn);
 }
}

聯繫我們

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