欧美国产另类久热|951超碰伊人久久|伊人影视久久久久|色婷婷亚洲小电影|色东京热69XX|婷婷玖玖深爱网|加勒比东京热网站|无码A级毛片在线观看|一级a毛一级a看|中文字幕人妻欧美

Jdom讀取數(shù)據庫并生成xml文件示例
來源:易賢網 閱讀:995 次 日期:2014-12-02 15:13:53
溫馨提示:易賢網小編為您整理了“Jdom讀取數(shù)據庫并生成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查詢數(shù)據庫把數(shù)據寫入xml文件中

* @author admin

*

*/

public class WriteXml {

public static void WriterFileToXml(){

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

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

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

Connection conn = DatabaseConnection.getConn();

String sql = "select * from user";

try {

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

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

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ù)據

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

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

} catch (SQLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

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

WriteXml.WriterFileToXml();

}

}

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

代碼如下復制代碼

package cn.gov.csrc.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

/**

* 連接數(shù)據庫

*

* @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技術專欄

更多信息請查看網絡編程
關于我們 | 聯(lián)系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權所有:易賢網