By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,287 Members | 1,284 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,287 IT Pros & Developers. It's quick & easy.

How to Update Blob with JDBC?

P: n/a
Hi,
I am accessing DB2 8.2 (Windows) via JDBC type 4 and am having
troubles making Blob.setBinaryStream(long) work to update a BLOB
column.
Here is the excerpt of code that I am using:

Connection conn = ds.getConnection();
conn.setAutoCommit(false);
// insert a file
FileInputStream fis = new FileInputStream(fileName);
PreparedStatement call =
conn.prepareStatement(
"insert into input_file (file_id, file_name,
content) " +
"values (?, ?, ?)");
call.setInt(1, 1);
call.setString(2, fileName);
call.setBytes(3, new byte[1]);
call.executeUpdate();
conn.commit();
call.close();
conn = ds.getConnection();
conn.setAutoCommit(false);
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(
"select content from Input_File where file_id = 1 for
update");
rs.next();
Blob bb = rs.getBlob("content");
OutputStream os = bb.setBinaryStream(0);
int dataByte = 0;
while ((dataByte = fis.read()) != -1) {
os.write(dataByte);
bytesWritten++;
}
os.close();
conn.commit();
rs.close();
stm.close();

the following is DDL for input_file table:
CREATE TABLE INPUT_FILE (
FILE_ID INTEGER NOT NULL,
FILE_NAME VARCHAR(100),
CONTENT BLOB(104857600)
)

The result of running the above code is that the record inserted,
however the content of the blob is just one byte long and does not
contain the file I am trying to put in the blob (content column). It
seems that the work done by the second statement does not have any
effects on the column.
Can anybody help please!
Thanks
Nesa

Jun 20 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.