我在oracle数据库有一个表,有两个字段,id和xml_c 分别为varchar2和xmltype类型,请问怎么编写程序应用jdbc从数据库中通过id查询到xml_c并且把它输出到指定的文件内,比如:c:/xml_out.xml
我的源程序如下:
public void myclass
{
try
  {     
   // register the Oracle JDBC drivers
      DriverManager.registerDriver(
        new oracle.jdbc.OracleDriver()
      );
   // EDIT IF NECESSARY
   // create a Connection object, and connect to the database
   // as store using the Oracle JDBC Thin driver
      myConnection = DriverManager.getConnection(
        "jdbc:oracle:thin:@localhost:1521:ORCL",
        "scott",
        "tiger"
      );
   // disable auto-commit mode
      myConnection.setAutoCommit(false);
   // create a Statement object
      myStatement = myConnection.createStatement();      
      File file=new File("C:/xml_out.xml");
      FileOutputStream fout=new FileOutputStream(file);
      
      String mysql="select xml_c from myxml where id=?";
      OraclePreparedStatement opst=(OraclePreparedStatement) myConnection.prepareStatement(mysql);
      opst.setString(1,"20070622001");
      OracleResultSet ors=(OracleResultSet)opst.executeQuery();
      if(ors.next())
      {
      // get the XMLType
      XMLType poxml = XMLType.createXML(ors.getOPAQUE(1));
      // get the XMLDocument
      Document podoc = (Document)poxml.getDOM();  
     // 这里有问题
      ((XmlDocument)podoc).write(fout);
     //?????????????????????????????       
      }
      ors.close();
      System.out.println("xml文件已经读出到指定文件内!");     
  }
  catch(Exception e)
  {
   System.out.println("error :"+e.toString() );
   e.printStackTrace();   
  }
请高手修改好此程序,能够正确输出好吗?或者有更好的方法也行

解决方案 »

  1.   

    dom4j如下:
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("dataset");Element element = root.addElement("row");
    element.addElement("xml_c").setText(ors.getOPAQUE(1));
    element.addElement("tempaccount").setText(account.getTempAccount()); // lets write to a file
    OutputFormat format = new OutputFormat();
    format.setEncoding("gb2312");
    XMLWriter writer = new XMLWriter(new FileWriter(fileName), format);
    writer.write(document);
    writer.close();
      

  2.   

    提下思路,
    第一步:先把数据查出来,放在一个List。
    第二步:利用XML的API进行操作,把数据写进去,很简单。
      

  3.   

    //element.addElement("tempaccount").setText(account.getTempAccount()); 多了
      

  4.   

    把读出的xml内容转话成string,怎么转化不用不用我说了把? 放在str里
    写的时候
    FileOutputStream fileoutputstream = new FileOutputStream(文件路径);
    byte bytes[] = strout.getBytes();
    fileoutputstream.write(bytes);
    最好先把事务去掉,调试好了在加上.
      

  5.   

    用string输出还是没有用啊,运行还是出现错误:Exception in thread "main" java.lang.NoClassDefFoundError: oracle/xml/parser/v2/XMLParseException
    at getxml.main(getxml.java:59)
    哪位高手再指点一下!
      

  6.   

    public void myclass
    {
    try
      {     
       // register the Oracle JDBC drivers
          DriverManager.registerDriver(
            new oracle.jdbc.OracleDriver()
          );
       // EDIT IF NECESSARY
       // create a Connection object, and connect to the database
       // as store using the Oracle JDBC Thin driver
          myConnection = DriverManager.getConnection(
            "jdbc:oracle:thin:@localhost:1521:ORCL",
            "scott",
            "tiger"
          );
       // disable auto-commit mode
          myConnection.setAutoCommit(false);
       // create a Statement object
          myStatement = myConnection.createStatement();      
          File file=new File("C:/xml_out.xml");
          FileOutputStream fout=new FileOutputStream(file);
          
          String mysql="select xml_c from myxml where id=?";
          OraclePreparedStatement opst=(OraclePreparedStatement) myConnection.prepareStatement(mysql);
          opst.setString(1,"20070622001");
          OracleResultSet ors=(OracleResultSet)opst.executeQuery();
          if(ors.next())
          {
          // get the XMLType
          XMLType poxml = XMLType.createXML(ors.getOPAQUE(1));
          // get the XMLDocument
          Document podoc = (Document)poxml.getDOM();  
          ((XmlDocument)podoc).write(fout);  
          }
          ors.close();
          System.out.println("xml文件已经读出到指定文件内!");     
      }
      catch(Exception e)
      {
       System.out.println("error :"+e.toString() );
       e.printStackTrace();   
      }