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