我在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.   

    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
               ((XmlDocument)podoc).write(fout);
             }
          ors.close();
          System.out.println("xml文件已经读出到指定文件内!");     
      }
      catch(Exception e)
      {
       System.out.println("error :"+e.toString() );
       e.printStackTrace();   
      }