2 messages in com.mysql.lists.javastreaming data to a blob
FromSent OnAttachments
Trevor Sutton11 Oct 2005 15:22 
Rhino11 Oct 2005 17:01 
Subject:streaming data to a blob
From:Trevor Sutton (tre@opecsystem.com)
Date:10/11/2005 03:22:09 PM
List:com.mysql.lists.java

Hi

I have been chasing myself around with this one, so any thoughts or suggestions will be kindly accepted.

Problem:

I want to serialize a JDOM Document to a blob.

My solution:

I thought I would do this as I have done with Oracle in the past

In Oracle you could select a record for update to obtain a Blob reference. From the Blob you could derive an output stream and then stream the data to the blob, like this

Blob blob = rs.getBlob( "someColumn" ); OutputStream os = blob.getOutputStream(); Os.write( data );

However, the MySQL Blob object does not seem to have a getOutputStream method. My Code is shown below, assuming that there IS a getOutputStream method for Blob.

/** * Method to update an information record and serialize the JDOM Document into * the Blob column. * @param pic, this is an object containing the informaion content key and the Document * to be serialized. * @return boolean, true no error, otherwise false */ private static boolean addContent(Tbl_process_info_content pic) { boolean result = false; String sql = "select content from tbl_process_info_content " + " where " + " info_content_id = ? " + " for update of content ";

DataStorage ds = DataStorage.getInstance(); Connection con = ds.getDbConnectionFromPool(); XMLOutputter outputter = new XMLOutputter(); Document doc = (Document)pic.getContent();

try { PreparedStatement stmt = con.prepareStatement(sql); stmt.setLong( 1, pic.getInformationContentId() ); ResultSet rs = stmt.executeQuery();

while (rs.next() ) { // get a refernce to the Blob Blob blob = rs.getBlob( "CONTENT" );

// now stream the data to the blob, first reference the output stream of the blob OutputStream os = blob.getBinaryOutputStream();

// next pass the output stream to the XMLoutputter outputter.output( doc, os); } stmt.close(); con.commit(); result = true; } catch (Exception ex) { Logger.getInstanceOf().outExceptionStackTrace("Exception while updating blob in information record- ", ex); } finally { ds.releaseConnectionToPool(con); } return result; }