working with blobs
2 posts in topic
Flat View  Flat View

Posted By:   kelly_benson
Posted On:   Sunday, November 3, 2002 09:52 PM

i am looking to put data into a blob, but am unsure how to go about it as the data is retrieved from a formfile and i keep getting type mismatches when i try to put the data in the blob.

if anyone could give me any tips on using blobs.

Re: working with blobs

Posted By:   michael_dean  
Posted On:   Tuesday, November 5, 2002 11:30 PM

A JDBC Blob is actually a "pointer" to a database resource. This allows us to use 4GB BLOB's without 4+GB of heap space.

To write to a Blob, you have to retrieve it from the database (just like retrieving any other data type), and then ask it for an output stream, and write data from the source (your FormFile's input stream) to the output stream.

The code will look something like this (this is all from memory and I don't have access to a database to test it out). Note that this code requires try ... catch blocks and should really use a finally to ensure that the non-memory resources (I/O and JDBC) are properly closed. However, in the interest of brevity, I left it off.

Connection conn = ...;
conn.setAutoCommit(false); // might work with true, but I highly recommend...
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT blob_col FROM table WHERE ...");
Blob data = null;
while (rs.hasNext()) {
data = rs.getBlob("blob_col");
// get the correct FormFile (we'll call it "file" like you did above)
InputStream is = file.getInputStream();
OutputStream os = data.getBinaryOutputStream();
int bufferSize = 4096; // preferrably the BLOB's CHUNKSIZE
int bytesRead = 0;
long bytesWritten = 0;
byte[] buffer = new byte[bufferSize];
while ((bytesRead =, 0, bufferSize)) != -1) {
os.write(buffer, 0, bytesRead);
bytesWritten += bytesRead;
// Close appropriate streams (do you need to close "is"?)
// Commit changes
// Close JDBC resources

Re: working with blobs

Posted By:   Scott_Yaung  
Posted On:   Sunday, November 3, 2002 11:00 PM

Can I view ur code ?
About | Sitemap | Contact