注重本系列文件使用环境:Oracle数据和JDOM1.0版本
总计四篇文件中都使用到以下表和数据。
Oracle表结构如下:
/*此表中最要害的就是CID和PID两个字段,其它的跟据需要可以增减字段*/
CREATE TABLE SCOTT.COMPANY
(
CID NUMBER(4) NOT NULL, /*记录ID号*/
CNAME VARCHAR2(20) NOT NULL, /*名字*/
DESCPT VARCHAR2(40) NULL, /*描述*/
PID NUMBER(4) NULL /*父ID号*/
);
/*向表中插入数据*/
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 1, ´中南迈特´, ´湖南省长沙市´, 0 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 2, ´系统集成´, ´各种系统集成´, 1 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 3, ´软件开发´, ´软件开发´, 1 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 6, ´NetOA开发组´, ´Net项目开发´, 3 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 7, ´王军´, ´J2EE组王军´, 5 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 8, ´湘红´, ´J2EE组湘红´, 5 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 9, ´Windows集成组´, ´Windows系统集成´, 2 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 10, ´Linux集成组´, ´Linux相关系统集成´, 2 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 11, ´王非´, ´Linux组´, 10 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 12, ´张万´, ´NetOA组´, 6 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 13, ´李兵´, ´J2EE李兵´, 5 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 14, ´武成´, ´Linux组´, 10 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 4, ´人事部´, ´公司人事治理部门´, 1 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 5, ´J2EE项目组´, ´J2EE项目开发´, 3 );
INSERT
INTO company( company.cid, company.cname, company.descpt, company.pid )
VALUES( 15, ´王义´, ´J2EE组王义´, 5 );
package jing.XML;
/**
* <p>Title: 从数据库中读取记录集到层叠XML文件</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 欧朝敬 13873195792
* @version 1.0
*/
//将数据库表输出为XML文档
import org.jdom.*;
import org.jdom.output.*;
import Java.sql.*;
import java.io.*;
public class dBToxmltree {
public String url = null;
public Connection conn = null;
public Document document = null;
public dbtoxmltree() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
url = "jdbc:oracle:thin:@192.168.128.250:1521:sample";
conn = DriverManager.getConnection(url, "scott", "tiger");
}
public void digui(int pid,Element element) throws Exception {
String sql = "select * from company where pid=" + pid;
PreparedStatement pstmt = conn.prepareStatement(
sql,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rmd = rs.getMetaData();
int colcount = rmd.getColumnCount();
while (rs.next()) {
Element element0 = new Element("DSTree");
for(int i=1;i<=colcount;i++){
element0.setAttribute(rmd.getColumnName(i),
(rs.getString(i) == null ? "" :
rs.getString(i)));
}
element0.setAttribute("open","false");
element.addContent(element0);
digui(rs.getInt("CID"),element0);
}
rs.close();
pstmt.close();
}
public static void main(String[] args) throws Exception {
dbtoxmltree dbxml = new dbtoxmltree();
Element root=new Element("DSTreeRoot");
dbxml.document=new Document(root);//创建文档ROOT元素
PreparedStatement pstmt = dbxml.conn.prepareStatement(
"select * from company order by cid",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rmd = rs.getMetaData();
int colcount = rmd.getColumnCount();
Element elementcol = new Element("COLTYPE");
for (int i = 1; i <= colcount; i++) { //列属性
elementcol.setAttribute(rmd.getColumnName(i),
rmd.getColumnTypeName(i));
}
root.addContent(elementcol);
rs.close();
pstmt.close();
dbxml.digui(0,root);
dbxml.conn.close();
XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); //格式华输出,产生缩进和换行
Format format = outp.getFormat();
format.setEncoding("GB2312"); //设置语言
format.setEXPandEmptyElements(true); //设置输出空元素为<sample></sample>格式
outp.setFormat(format);
outp.output(dbxml.document, new FileOutputStream("companytree.xml")); //输出XML文档
System.out.print("XML 文档生成完毕!");
}
}