• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

从数据库中读取记录集到层叠式XML

发布: 2007-7-14 21:19 | 作者: 佚名    | 来源: 网络转载     | 查看: 17次 | 进入软件测试论坛讨论

领测软件测试网数据库中读取记录集到层叠式XML

注意本系列文件使用环境: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 文档生成完毕!");
}
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网