【问题标题】:From an XMl file, an xml element data needs to be put into a clob column in oracle table从 XML 文件中,需要将 xml 元素数据放入 oracle 表中的 clob 列中
【发布时间】:2015-05-12 14:23:26
【问题描述】:

我有一个 xml 文件,我需要从中获取元素信息到 oracle 表中的 clob 列中。 xml文件是这样的:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<bookstore>
  <notepad>
   Test Notepad data. Test Notepad data. 
  </notepad>
  <documents>
   werfjkhrgkhrekgjeirogvmj vreuhfgiurvhe kjhr ieu
   ndbcjhaejvh ukarehv khrkuehgvuhwrhuivg hrfjkhuirehv
  </documents>
</bookstore>

记事本元素信息必须进入oracle表的clob列,文档元素信息必须进入同一张表的blob列。

编写的代码如下:

    **String notes = report.getNotepadInfo();
    System.out.println("the notes is : "+notes);**

    byte[] documentValByteArr = report.getDocumentsList().get(0).getDocumentValues().get(0).getDocumentValue().getBytes();                          
    System.out.println("documentValByteArr :"+documentValByteArr);

    //Pass the String notes to load as clob and Pass the byte[] to load as blob to LoadData class's updateDV method to update in the database.
    LoadData.updateDV(id,documentValByteArr,notes);

    enter code here 

    public static void updateDCNVersion(String Id, byte[] blob, String clob) {
      **ByteArrayInputStream bais = new ByteArrayInputStream(blob);**

        // Here we run the Function to update data in the table.
        try {
            CallableStatement proc_stmt = connection.prepareCall(" {call P_UPDATE_DV(?,?,?)}");
            proc_stmt.setString(1,Id);
                     proc_stmt.setBinaryStream(2, bais, (int)blob.length);
        proc_stmt.setString(3, clob);
                proc_stmt.executeQuery();
            proc_stmt.close();
        } catch (SQLException e) {
            System.out.println("ERROR: SQL Exception updating T_DV");
            System.err.println("SQLState: " + e.getSQLState());
            e.printStackTrace();
        }       
    }

这种将字符串数据加载到 oracle 表中的 clob 列的方法非常有效。 但是 Byte[] 数据没有加载到 oracle 表的 blob 列中?

请查看更改后的代码并告诉我。 谢谢楼主。

【问题讨论】:

    标签: java


    【解决方案1】:

    试试下面的代码

    byte[] documentValByteArr = ..........    
    ByteArrayInputStream bais = new ByteArrayInputStream(documentValByteArr );
    pstmt.setBinaryStream(1, bais , (int)documentValByteArr .length());
    

    或者试试这个

    这两个BLOB不兼容你可以试试

     weblogic.jdbc.common.OracleBlob
    

    【讨论】:

    • 这是一个 Java 控制台项目。我没有使用任何 weblogic。
    • 我说的是jar,不是应用服务器。
    • 我正在使用 ojdbc14.jar。
    • 我应该使用不同的 JAR。
    • @user2015 我用不同的方法更新了我的答案,它必须对你有用,而且你不需要更改 jar 只需尝试上面的代码(方法 1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多