如何将xml文件作为CLOB保存到oracle数据库中而不用在java中保存到操作系统

时间:2017-04-26 14:35:10

标签: java xml oracle clob

我完成了对XML文件进行数字签名的任务,该文件位于oracle数据库表中并再次插入到数据库中。我是通过三个步骤来完成的:

  1. 从数据库中获取XML文件(保存为CLOB)。
  2. 数字签名。
  3. 将其保存到某个目录中作为actualfile.xml。
  4. 读取该文件并将其作为CLOB保存到数据库中。
  5. 问题是我想要将其保存到目录文件夹中(步骤3)。这是我正在使用的代码:

     private static void storeSignedDoc(Document doc) throws ClassNotFoundException, SQLException, FileNotFoundException {
    //    public static FileOutputStream storeSignedDoc(Document doc) {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer trans = null;
            FileOutputStream fileOutputStream = null;
            try {
                trans = transFactory.newTransformer();
            } catch (TransformerConfigurationException ex) {
                ex.printStackTrace();
            }
            try {
                Document signedXmlFileDoc = null;
                String destnSignedXmlFilePath = "383_ALLA_200417_001_signed.xml";
                StreamResult streamRes = new StreamResult(new File(destnSignedXmlFilePath));
    
    //            StreamResult streamRes = new StreamResult(fileOutputStream);
                trans.transform(new DOMSource(doc), streamRes);
    
    //            trans.transform(new DOMSource(doc), new Result(signedXmlFileDoc));
                System.out.println("fileOutputStream : " + fileOutputStream);
                ClobFile cb = new ClobFile();
                cb.insertClob();
            } catch (TransformerException ex) {
                ex.printStackTrace();
            }
            System.out.println("XML file with attached digital signature generated successfully ...");
    //        return fileOutputStream;
        }//storeSignedDoc() ends here
    

    用于检索和插入xml文件:

    public InputStream getByteByClob(String query) throws ClassNotFoundException, SQLException {
            Clob clob = null;
    ResultSet rs = cs.connect(query);
            while (rs.next()) {
                clob = rs.getClob(1);
            }
    InputStream clobStream = clob.getAsciiStream();
    return clobStream;
        }
    
    public void insertClob() throws ClassNotFoundException, SQLException, FileNotFoundException {
    
    String query = "---UPDATE QUERY ----";
    Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("-- AS REQUIRED --");
            PreparedStatement pstmt = con.prepareStatement(query);
    
            File f = new File("myfile_signed.xml");
            FileReader fr = new FileReader(f);
    
            pstmt.setCharacterStream(1, fr, (int) f.length());
            int i = pstmt.executeUpdate();
            System.out.println(i + " records affected");
    
            con.close();
        }
    } 
    

    请帮我解决这个问题..

0 个答案:

没有答案