Re: INSERTINg a Clob in a portable way



On Aug 14, 10:29 am, Thomas Kellerer <FJIFALSDG...@xxxxxxxxxxxxx>
wrote:
Hello,

I'm trying to insert a values for a CLOB column in a portable way. So far using
PreparedStatement.setCharacterStream() together with a Reader to read the CLOB
contents from a local file was working quite well (with various JDBC drivers).

Now I stumbled across a bug in my code that raised it ugly head when using
Apache Derby.

When reading the contents of a file with a multi-byte encoding I can't seem to
find a way to pass the correct length of the data into the setCharacterStream()
method.

What I did so far:

File f = new File("sourcfile.txt");
Reader r = new InputStreamReader(new FileInputStream(f), "UTF-8");
preparedStatement.setCharacterStream(1, r, f.length());

which worked fine for most JDBC drivers except for Derby as Derby (rightfully)
complains that the value supplied (f.length()) does not match the number of
characters read from the stream.

I can't image a way where I can supply the correct length when using a Reader to
supply the Clob content. If I was able to "create" Clob instance in a generic
way, I think this could solve the problem as the Clob interface contains a
setCharacterStream() which does not require a length parameter.
But how would I create the Clob instance if I don't know which driver will be
used at runtime?

Any input appreciated.

Thanks in advance
Thomas

Hi. How about trying setBinaryStream() and send the data as raw bytes?
Joe

.



Relevant Pages

  • INSERTINg a Clob in a portable way
    ... So far using PreparedStatement.setCharacterStreamtogether with a Reader to read the CLOB contents from a local file was working quite well (with various JDBC drivers). ... Now I stumbled across a bug in my code that raised it ugly head when using Apache Derby. ...
    (comp.lang.java.databases)
  • Re: some problem with CLOB
    ... put some news information which may be larger than 4k, ... initialize a CLOB variable and select the clob field for update ... // read from the stream into a String variable ... I think that is supported by newer drivers as well. ...
    (comp.lang.java.programmer)