以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML 与 数据库 』  (http://bbs.xml.org.cn/list.asp?boardid=17)
----  求助::论文急需解决的问题。。。。急急急急  (http://bbs.xml.org.cn/dispbbs.asp?boardid=17&rootid=&id=62755)


--  作者:guoyanhong
--  发布时间:5/18/2008 4:03:00 PM

--  求助::论文急需解决的问题。。。。急急急急

请问一下。。。为什么下面的程序会有错误呢?
package XMLViewGenerator;

import org.jdom.*;
import org.jdom.output.*;
import java.sql.*;
import java.io.*;
import java.util.*;

public class DefaultXMLView {
    
    private java.sql.Connection con = null;
    private final String url = "jdbc:microsoft:sqlserver://";
    private final String serverName= "localhost";
    private final String portNumber = "1433";
    private final String databaseName= "Collage";
    private final String userName = "sa";
    private final String password = "";
    private final String selectMethod = "cursor";
    
    /** Creates a new instance of DefaultXMLView */
    public DefaultXMLView() {
    }
    private String getConnectionUrl() {
        return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";"; }
    private java.sql.Connection getConnection() {
        try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
        if(con!=null)
            System.out.println("成功连接数据库:"+databaseName); } catch(Exception e) {
                e.printStackTrace();
                System.out.println("跟踪在方法getConnection()出现的错误 : " + e.getMessage()); }
        return con; }
    
    private void closeConnection() {
        try {
            if(con!=null)
                con.close();
            con=null; } catch(Exception e){ e.printStackTrace(); } }
    public void GenerateDefaultView() {
        DatabaseMetaData dbMetaData = null;
        ResultSet TableSet = null;
        ResultSet rs = null;
        ResultSetMetaData rmd = null;
        String RootName=databaseName;
        Document XMLDocument = new Document(new Element(RootName)); //创建文档ROOT元素,ROOT元素名即为数据库名
        try{con= this.getConnection();
        if(con!=null) {
            dbMetaData=con.getMetaData();
            ArrayList TableList=new ArrayList();
            TableSet=dbMetaData.getTables(null,null,null,null);//查找出数据库中所有表的名称,包括系统表和用户表
            int ListSize;
            while(TableSet.next()) {
                TableList.add(TableSet.getString("TABLE_NAME"));//将表的名称存入到ArrayList中
            }
            ListSize=TableList.size();
            for(int i=0;i<ListSize;i++) {
                //从ArrayList中依次取出每个表,然后用SELECT查询表中的所有字段
                String SQLInstruction="SELECT * FROM"+" "+TableList.get(i).toString();
                PreparedStatement pstmt = con.prepareStatement(SQLInstruction,
                        ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                //将表名作为元素名
                Element TableElement = new Element(TableList.get(i).toString());
                rs = pstmt.executeQuery();
                rmd = rs.getMetaData();//获取元数据,如属性名等
                int colcount = rmd.getColumnCount();//获取属性列的个数
                while (rs.next()) {
                    Element RowElement = new Element("ROW");
                    //每一个元组构成一个元素“ROW”,“ROW”中的子元素就是元组中的属性
                    //取出元组中的每一个属性名和属性的取值
                    //属性名作为元素名,属性的取值作为元素的内容
                    for (int j = 1; j <= colcount; j++) {
                        Element TempElement=new Element(rmd.getColumnName(j).toString());
                        TempElement.setText(rs.getString(j));
                        RowElement.addContent(TempElement);
                    }
                    //将每一个“ROW”元素作为以表名作为元素名元素的子元素
                    TableElement.addContent(RowElement);
                }
                //添加到根元素中
                XMLDocument.getRootElement().addContent(TableElement);
                pstmt.close();
            }
            //关闭数据库连接
            rs.close();
            rs = null;
            closeConnection();
            
            XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); //格式化输出,产生缩进和换行
            
            Format format = outp.getFormat();
            format.setEncoding("GB2312"); //设置语言
            format.setExpandEmptyElements(true); //设置输出空元素格式
            outp.setFormat(format);
            
            outp.output(XMLDocument, new FileOutputStream(databaseName+".XML"));//输出XML文档
            System.out.print("XML 文档生成完毕!");
        } else
            System.out.println("错误: 没有可用的数据库连接!");} catch(Exception e){ e.printStackTrace(); }
    }
    public static void main(String[] args) throws Exception {
        DefaultXMLView myDefaultView = new DefaultXMLView();
        myDefaultView.GenerateDefaultView();
    }
    
}

为什么这块地方总是会有红色的灯泡呢?         
            Format format = outp.getFormat();
            format.setEncoding("GB2312"); //设置语言
            format.setExpandEmptyElements(true); //设置输出空元素格式
            outp.setFormat(format);

请大家指导一下。。。程序急,,,,论文急需。。。。。谢谢!!!!!
我的QQ:253865038
希望能够交流,交流;


--  作者:shinel111
--  发布时间:5/19/2008 7:50:00 PM

--  
同学做好了?
是不是少了个包啊?

我也在做~
你有 xml到数据库吗?


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms