Jdom讀取數(shù)據(jù)庫并生成xml文件示例
來源:易賢網(wǎng) 閱讀:758 次 日期:2014-12-02 15:13:53
溫馨提示:易賢網(wǎng)小編為您整理了“Jdom讀取數(shù)據(jù)庫并生成xml文件示例”,方便廣大網(wǎng)友查閱!

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查詢數(shù)據(jù)庫把數(shù)據(jù)寫入xml文件中

* @author admin

*

*/

public class WriteXml {

public static void WriterFileToXml(){

//創(chuàng)建一個xml文件

File file = new File("D://user.xml");

//創(chuàng)建數(shù)據(jù)庫連接

Connection conn = DatabaseConnection.getConn();

String sql = "select * from user";

try {

Statement st = conn.createStatement();//創(chuàng)建預(yù)處理對象

ResultSet rs = st.executeQuery(sql);//獲得結(jié)果集

Element root = new Element("users");//創(chuàng)建根元素

while (rs.next()) {

Element user = new Element("user");//創(chuàng)建子元素

root.addContent(user);//添加子元素到根節(jié)點

Element id = new Element("ID");//創(chuàng)建葉子節(jié)點

id.setText(rs.getString("ID"));//給葉子節(jié)點賦值

user.addContent(id);//添加葉子節(jié)點到父節(jié)點

Element ename = new Element("USERNAME");

ename.setText(rs.getString("USERNAME"));//給葉子節(jié)點賦值

user.addContent(ename);

Element password = new Element("PASSWORD");//創(chuàng)建葉子節(jié)點

password.setText(rs.getString("PASSWORD"));//給葉子節(jié)點賦值

user.addContent(password);//添加葉子節(jié)點到父節(jié)點

Element status = new Element("STATUS");

status.setText(rs.getString("STATUS"));//給葉子節(jié)點賦值

user.addContent(status);

Element descn = new Element("DESCN");

descn.setText(rs.getString("DESCN"));//給葉子節(jié)點賦值

user.addContent(descn);

}

Document doc = new Document();//創(chuàng)建文本對象

doc.addContent(root);//添加樹倒文本中

Format format= Format.getCompactFormat();

format.setIndent(" ");

XMLOutputter out = new XMLOutputter(format);//創(chuàng)建輸出流

FileWriter fw = new FileWriter(file);//寫數(shù)據(jù)

out.output(doc, fw);//輸出到xml文件中

fw.close();//關(guān)閉寫入流

} catch (SQLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

//把數(shù)據(jù)庫的數(shù)據(jù)寫入xml文件中

WriteXml.WriterFileToXml();

}

}

  2.連接數(shù)據(jù)庫java類:DatabaseConnection.java:

代碼如下復(fù)制代碼

package cn.gov.csrc.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

/**

* 連接數(shù)據(jù)庫

*

* @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);

}

}

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:Jdom讀取數(shù)據(jù)庫并生成xml文件示例
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)