title: Uploading Large Text File Into A DB2 CLOB Field
body: <p>Alrighty, I've been banging my head against the wall on this one for a while, so here it goes...</p>
  
  <p>I'm currently writing a Java (1.5) program that can upload very large (200MB+) text files to a DB2 CLOB Column, but have been running into a few issues.</p>
  
  <p>During my research, several code examples suggested that I use the PreparedStatement method <code>setCharacterStream()</code> to upload data to the CLOB Field, giving me something like this:</p>
  
  <pre><code> PreparedStatement preparedStatement =  
        connection.prepareStatement("INSERT INTO BOOKCOVERS VALUES(?,?)"); 
  File imageFile = new File("c:\\redbookcover.jpg"); 
  InputStream inputStream = new FileInputStream(imageFile); 
  preparedStatement.setString(1," 0738425826"); 
  preparedStatement.setBinaryStream(2,inputStream,(int)(imageFile.length())); 
  preparedStatement.executeUpdate(); 
  </code></pre>
  
  <p>This is fine, except for the fact that the <code>setCharacterStream()</code> method requires a LENGTH value, specifying the number of characters to write to the field.  With this in mind, this limits the number of characters that can be written to CLOB Field to <code>Integer.MAX_VALUE</code>.  Doing a little math, this will limit the size of the text file that I can upload to 524288 bytes, or about half of a Megabyte.  Since a CLOB field can store up to 2GB of data, I'm a bit confused.</p>
  
  <p>I see that in Java 1.6 the PreparedStatement's <code>setCharacterStream()</code> method has been modified to not require a length, and will read until end-of-file is encountered.  Unfortunately, I'm required to use Java 1.5, so this is not a valid option.</p>
  
  <p>In one last bit of research, I read that Java 1.5's PreparedStatement has similar functionality, where the Javadoc is claiming:</p>
  
  <blockquote>
    <p>"...it may be more practical to send
    it via a java.io.Reader object. The
    data will be read from the stream as
    needed until end-of-file is reached."</p>
  </blockquote>
  
  <p>If this exists, why does the <code>setCharacterStream()</code> method still require a length parameter?  What would you pass in for the length parameter to use this functionality?  Is there a better way to do this that I'm missing?</p>
  
  <p>Any thoughts?</p>