Insert CLOB into Oracle database

后端 未结 6 2252
孤独总比滥情好
孤独总比滥情好 2020-12-03 16:32

My question is: How do you get around the ORA-01704: string literal too long error when inserting (or doing anything in queries) with CLO

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 17:01

    From Oracle document

    You must bear in mind the following automatic switching of the input mode for large data. There are three input modes as follows: Direct binding, Stream binding, and LOB binding.

    For PL/SQL statements

    The setBytes and setBinary stream methods use direct binding for data less than 32767 bytes.

    The setBytes and setBinaryStream methods use LOB binding for data larger than 32766 bytes.

    The setString, setCharacterStream, and setAsciiStream methods use direct binding for data smaller than 32767 bytes in the database character set.

    The setString, setCharacterStream, and setAsciiStream methods use LOB binding for data larger than 32766 bytes in the database character set.

    The setBytesForBlob and setStringForClob methods, present in the oracle.jdbc.OraclePreparedStatement interface, use LOB binding for any data size.

    Follow is a example for put a file content into a input CLOB parameter of a PLSQL procedure:

      public int fileToClob( FileItem uploadFileItem ) throws SQLException, IOException
      {
        //for using stmt.setStringForClob method, turn the file to a big String 
        FileItem item = uploadFileItem;
        InputStream inputStream = item.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader( inputStream ); 
        BufferedReader bufferedReader = new BufferedReader( inputStreamReader );    
        StringBuffer stringBuffer = new StringBuffer();
        String line = null;
    
        while((line = bufferedReader.readLine()) != null) {  //Read till end
            stringBuffer.append(line);
            stringBuffer.append("\n");
        }
    
        String fileString = stringBuffer.toString();
    
        bufferedReader.close();         
        inputStreamReader.close();
        inputStream.close();
        item.delete();
    
        OracleCallableStatement stmt;
    
        String strFunction = "{ call p_file_to_clob( p_in_clob => ? )}";  
    
        stmt= (OracleCallableStatement)conn.prepareCall(strFunction);    
    
        try{    
          SasUtility servletUtility = sas.SasUtility.getInstance();
    
          stmt.setStringForClob(1, fileString );
    
          stmt.execute();
    
        } finally {      
          stmt.close();
        }
      }
    

提交回复
热议问题