6

I am currently validating an application developed on Oracle for DB2. Since we don't want to maintain two separate sources, I need some query to insert blob into a field, that works in both oracle and db2. I don't have any identifier to distinguish under which DB the application is running.

I used utl_raw.cast_to_raw in oracle and CAST() as BLOB in DB2 which are mutually incompatible.

1 Answer 1

7

You won't be able to find a common SQL that uses some kind of casting. But you can do this with "plain" SQL using JDBC's setBinaryStream()

PreparedStatement pstmt = connection.prepareStatement(
   "insert into blob_table (id, blob_data) values (?, ?)";

File blobFile = new File("your_document.pdf");
InputStream in = new FileInputStream(blobFile);

pstmt.setInt(1, 42);
pstmt.setBinaryStream(2, in, (int)blobFile.length());
pstmt.executeUpdate();
connection.commit();

You can use setBinaryStream() the same way with an UPDATE statement.

Sign up to request clarification or add additional context in comments.

3 Comments

I have the data inside a variable of type String. Is it fine to use the above method by converting the String to BinaryStream??
@Saju: how can you have a binary large object as a String? What is the actual (e.g. Oracle) datatype for that column? If that is a CLOB (rather than a BLOB) you should use setCharacterStream() instead of setBinaryStream()
Perhaps @Saju means NCLOB in Oracle, DBCLOB in DB2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.