Reading a Blob!

 Note: tested with Oracle DB 9.x and a thin driver 9.x !

import groovy.sql.Sql

println "---- A working test of writing and then reading a blob into an Oracle DB ---"
sql = Sql.newInstance("jdbc:oracle:thin:@pignut:1521:TESTBNDY", "userName",
                     "paSSword", "oracle.jdbc.OracleDriver")

rowTest = sql.firstRow("select binarydata from media where mediaid = 11122345")
blobTest = (oracle.sql.BLOB)rowTest[0]

byte_stream_test = blobTest.getBinaryStream()
if( byte_stream_test == null ) {  println "Test: Received null stream!"  }

byte[] byte_array_test = new byte[10]
int bytes_read_test = byte_stream_test.read(byte_array_test)

print "Read $bytes_read_test bytes from the blob!"

sql.connection.close()

Uses a table:

CREATE TABLE MEDIA
(
   MEDIAID NUMBER(22) NOT NULL,
   BINARYDATA BLOB NOT NULL
);

CREATE   SEQUENCE   SEQ_MEDIAID
INCREMENT   BY   1
START   WITH   100
ORDER;


 Copying A Blob to A File!

...
   byte_stream_test = blobTest.getBinaryStream()
   if( byte_stream_test == null ) {  println "Test: Received null stream!"  }

   blob_size = blobTest.length()
   println "Blob size: $blob_size"

   byte[] byte_array_test = new byte[blob_size]
   int bytes_read_test = byte_stream_test.read(byte_array_test)
   println "Read $bytes_read_test from the blob!"

   // Write to a file
   def fos= new FileOutputStream('c:\\Jornada\\auxil\\output.jpg')
   fos.write(byte_array_test);
   fos.close()
   ...