Hi Paul,

i came across an intresting article in JGuru hop this helps...personally the
new type4 driver from MS sucks..i prefer the weblogic type 4 or the
sprinta2000 type 4 driver....the MS driver has a lot of bugs with executing
stored procedures,multiple resultsets scrollable resultset etc...almost as
bad as the JDBC-ODBC bridge driver........

How do I extract a BLOB from a database?
Location: http://www.jguru.com/faq/view.jsp?EID=1325
Created: Nov 29, 1999 Modified: 2002-03-23 20:02:48.66
Author: Lennart Jorelid (http://www.jguru.com/guru/viewbio.jsp?EID=15)

A BLOB (Binary Large OBject) is essentially an array of bytes (byte[]),
stored in the database. You extract the data in two steps:

Call the getBlob method of the Statement class to retrieve a java.sql.Blob
object
Call either getBinaryStream or getBytes in the extracted Blob object to
retrieve the java byte[] which is the Blob object.
Note that a Blob is essentially a pointer to a byte array (called LOCATOR in
database-talk), so the java.sql.Blob object essentially wraps a byte
pointer. Thus, you must extract all data from the database blob before
calling commit or


private void runGetBLOB()
{
     try
     {   // Prepare a Statement:
         PreparedStatement stmnt = conn.prepareStatement("select aBlob from
BlobTable");

         // Execute
         ResultSet rs = stmnt.executeQuery();

         while(rs.next())
         {
            try
            {
               // Get as a BLOB
               Blob aBlob = rs.getBlob(1);
               byte[] allBytesInBlob = aBlob.getBytes(1, (int)
aBlob.length());
            }
            catch(Exception ex)
            {
               // The driver could not handle this as a BLOB...
               // Fallback to default (and slower) byte[] handling
               byte[] bytes = rs.getBytes(1);
            }
         }

       // Close resources
       rs.close();
       stmnt.close();

     }
     catch(Exception ex)
     {
       this.log("Error when trying to read BLOB: " + ex);
     }
}



Comments and alternative answers

 getBytes() position starts with 1.
Author: Joseph Shelby (http://www.jguru.com/guru/viewbio.jsp?EID=26292), Apr
30, 2001
The specification (javadocs) of java.sql.Blob.getBytes() states: Parameters:
pos - the ordinal position of the first byte in the BLOB value to be
extracted; the first byte is at position 1
Seems much of jdbc is designed for database guys who think the world starts
with 1, rather than java/c/c++ programmers who know better... ;-)
--Joe


  Re: getBytes() position starts with 1.
Author: Joe Sam Shirah (http://www.jguru.com/guru/viewbio.jsp?EID=42100),
Mar 23, 2002
Thanks, Joe. I have corrected the code to start at 1. And, yes, most JDBC
origins are at 1 rather than zero as you point out.

regards,
ravi


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to