Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert BLOB using java for both DB2 and Oracle

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.

like image 817
Saju Avatar asked May 09 '13 12:05

Saju


1 Answers

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.

like image 115
a_horse_with_no_name Avatar answered Sep 18 '22 11:09

a_horse_with_no_name