467,185 Members | 1,226 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,185 developers. It's quick & easy.

How to Update Blob with JDBC?

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
  • viewed: 4867
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by sime | last post: by
1 post views Thread by sid_mulpuru | last post: by
7 posts views Thread by Jean-Marc Blaise | last post: by
4 posts views Thread by alan_sec | last post: by
8 posts views Thread by Godzilla | last post: by
2 posts views Thread by jith87 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.