Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

exception getting blob to byte[]

818772Apr 18 2011 — edited Apr 21 2011
Hy guys,
I 've a problem with my java application.
I use hibernate to interact with a derby database.
I stored a image into blob field (and no problem).
When I try to get blob to array of byte I've this exception:
java.sql.SQLException: You cannot invoke other java.sql.Clob/java.sql.Blob methods after calling the free() method or after the Blob/Clob's transaction has been committed or rolled back.
To get blob I did
Blob cThumnb = ((Allegato) cAllegati.get(i)).getThumb();
byte[] cPrev = toByteArray(cThumnb);
where
private byte[] toByteArray(Blob fromBlob) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            return toByteArrayImpl(fromBlob, baos);
        }
        catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
        finally
        {
            if (baos != null)
            {
                try
                {
                    baos.close();
                }
                catch (IOException ex)
                {
                }
            }
        }
    }

    private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos)  throws SQLException, IOException
    {
        byte[] buf = new byte[4000];
        
        InputStream is = fromBlob.getBinaryStream();
        try
        {
            for (;;)
            {
                int dataSize = is.read(buf);
                if (dataSize == -1)     break;
                baos.write(buf, 0, dataSize);
            }
        }
        catch(IOException ex)
        {
                throw ex;
        }
        
       finally
        {
            if (is != null)
            {
                try
                {
                    is.close();
                }
                catch (IOException ex)
                {
                }
            }
        }
                return  buf;// baos.toByteArray();
    }
Could you help me?
I set also autocommit to false,
Thanks,
Regards
This post has been answered by EJP on Apr 18 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 18 2011
Added on Apr 18 2011
7 comments
639 views