Oracle JDBC Clob 插入问题 ORA-00942

Posted

技术标签:

【中文标题】Oracle JDBC Clob 插入问题 ORA-00942【英文标题】:Oracle JDBC Clob insert issue ORA-00942 【发布时间】:2015-07-28 14:15:01 【问题描述】:

我正在从 Oracle 源读取一组字段并将检索到的数据插入另一个 Oracle 源,当我尝试插入 clob 区域时,我碰巧遇到“java.sql.SQLException: ORA-00942: table or view不存在”异常,所有字段都插入成功,除了clob区域。下面我将添加目标表的字段和我的示例代码。我还发现了这个 solution on *** 并没有帮助我。目标表是oracle exadata

Table PODS.RAP_AUDIT
Column name: EVENT_ID
Column type: VARCHAR2

Column name: RUN_DATE
Column type: DATE

Column name: USER_NAME
Column type: VARCHAR2

Column name: STATUS
Column type: VARCHAR2

Column name: UNIVERSE_NAME
Column type: VARCHAR2

Column name: REPORT_NAME
Column type: VARCHAR2

Column name: CUI_ID
Column type: VARCHAR2

Column name: QUERY_NAME
Column type: VARCHAR2

Column name: REPORT_DURATION
Column type: NUMBER

Column name: ROW_COUNT
Column type: VARCHAR2

Column name: SQL
Column type: CLOB

代码:

private static void retrieveClobArea() 

        String sql = "select E.START_TIME as dateval, 5087472590231472273 as idval,  D.EVENT_DETAIL_VALUE as clobval  from BO41AUD.ADS_EVENT_DETAIL D, BO41AUD.ADS_EVENT E where E.EVENT_ID=D.EVENT_ID and     E.START_TIME >= to_date('28.07.2015 12:19:14','DD.MM.YYYY HH:MI:SS')";

        try 
            Class.forName(jdbcDriver);
            connection = DriverManager.getConnection(sourceDBConnection,
                    sourceDBUser, sourceDBPass);
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) 

                insertSampleCLOB(resultSet.getString(2), resultSet.getClob(3));
            
         catch (SQLException e) 
            System.err.println(e);
         catch (ClassNotFoundException e) 
            System.err.println(e);
         finally 
            try 
                connection.close();
             catch (SQLException e) 
                System.err.println(e);
            
        

    


private synchronized static void insertSampleCLOB(String eventId, Clob sql) 
        StringBuilder insertQuery = new StringBuilder();
        insertQuery.append("INSERT INTO PODS.RAP_AUDIT ");
        insertQuery.append("(");
        insertQuery.append("EVENT_ID, ");
        insertQuery.append("SQL");
        insertQuery.append(")  VALUES(?,?)");

        try 
            Class.forName(jdbcDriver);
            connection = DriverManager.getConnection(targetDBConnection,
                    targetDBUser, targetDBPass);
            preparedStatement = connection.prepareStatement(insertQuery
                    .toString());

            preparedStatement.setString(1, eventId);

            preparedStatement.setClob(2, sql);


            preparedStatement.executeUpdate();
            System.out.println("Inserted " + eventId + "  " + new java.util.Date());

         catch (ClassNotFoundException e) 
            logger.error("insertInRAP_AUDIT exception cnfe " + e);
            System.err.println(e);
         catch (SQLException e) 
            logger.error("insertInRAP_AUDIT exception sqle " + e);
            System.err.println(e);
         finally 
            try 
                connection.close();
             catch (SQLException e) 
                logger.error("insertInRAP_AUDIT exception connection close e "
                        + e);
                System.err.println(e);
            
        
    

【问题讨论】:

为什么链接问题中的解决方案没有帮助?您是否仍然使用setString 或字符流获得 ORA-00942,这表明您连接的用户只是无权访问表并且与 CLOB 无关?还是别的什么? 【参考方案1】:

在调用方法 insertSampleClob 之前,请执行以下操作:

Clob clob = CLOB.createTemporary(connection, false, oracle.sql.CLOB.DURATION_SESSION);
clob= resultSet.getClob(3);

然后:

insertSampleCLOB(resultSet.getString(2),clob);

它应该可以解决您的问题。

【讨论】:

我认为这只会泄漏一个临时 LOB,直到会话关闭 您正在创建一个临时 LOB,然后丢弃其定位器以存储来自 resultSet 的定位器。

以上是关于Oracle JDBC Clob 插入问题 ORA-00942的主要内容,如果未能解决你的问题,请参考以下文章

oracle.jdbc.OracleDatabaseException:ORA-01400:无法将 NULL 插入

如何通过sql的insert语句插入大量字符串到oracle的clob字段?

ORA-01704 错误,插入长度超过 4000 个字符的字符串

Oracle:在 UNION 语句 ORA-00932 中,clob 列与自身不一致

插入 CLOB 列时出错:ORA-06502:PL/SQL:数字或值错误

Oracle 存储过程因复杂类型的 clob 而失败,并带有 ORA-22922