posted 18 years ago
Hi,
I have solved this problem by inserting empty_blob() while inserting the BLOB image. Retrieve the row just inserted, and lock it for insertion of the BLOB columns.
prapredStatement = connection.prepareStatement("select image from image_upload where log_id = ? and image_name = ? for update");
prapredStatement.setInt(1,logId);
prapredStatement.setString(2,imageName);
// Execute the SQL statement
resultSet = prapredStatement.executeQuery();
// Retrieve Blob streams for Image column, and load the sample file
if(resultSet.next())
{
// Get the Blob locator and open output stream for the Blob
Blob mapBlob = resultSet.getBlob(1);
OutputStream blobOutputStream = ((oracle.sql.BLOB)mapBlob).getBinaryOutputStream();
// Open the FileInputStream for insertion into the Blob column
// saveTo is File from which where you are getting the image
InputStream sampleFileStream = new FileInputStream(saveTo);
// Buffer to hold chunks of data to being written to the Blob.
byte[] buffer = new byte[10* 1024];
// Read a chunk of data from the sample file input stream, and write the chunk to the Blob column output stream.
//Repeat till file has been fully read.
int nread = 0; // Number of bytes read
while( (nread= sampleFileStream.read(buffer)) != -1 ) // Read
//from file
blobOutputStream.write(buffer, 0, nread); // Write to Blob
// Close both streams
sampleFileStream.close();
blobOutputStream.close();
}