1

I want to save an image in an Oracle DB as a BLOB.

When trying to do this, I get this error message

java.sql.SQLException: ORA-01465: invalid hex number

My code snippet:

   ...
   Blob blob = con.createBlob(); 
   blob.setBytes(1, myByteArray); // blob = oracle.sql.BLOB@7543e69b

   String query = "update pictures set pic = \'" + blob + "\' where txt = \'Photo1\'";

   statement.executeQuery(query);
   ...

Next I tried a new version, in which I only inserted the hex number(7543e69b) behind "oracle.sql.BLOB@" in the DB.

That worked, and after reading out the blob I have a blob object again.

But after convert this blob object to icon, can no longer display an image. But after converting from blob to icon, the image is no longer displayed in the GUI. And when debugging, the size of the image is, for example, width: -1, height: -1, ....

This code snippet:

        ...
        Blob blob = con.createBlob(); 
        blob.setBytes(1, myByteArray); // blob = oracle.sql.BLOB@7543e69b

        String picture = blob + "";          
        picture = picture.substring(16); // picture = 7543e69b

        String query = "update pictures set pic = \'" + picture + "\' where txt = \'Photo1\'";
        statement.executeQuery(query);
        ...

        Blob a = rs.getBlob(4);  

        ImageIcon imageIcon = null;

        try {
            byte[] bArray = a.getBytes((long) 1, (int) a.length());
            imageIcon = new ImageIcon(bArray);

        } catch (Exception ex) {
            ...
        }
        photo.setIcon(imageIcon);

What is going wrong here? Is the saving wrong?

Hans
  • 49
  • 5
  • 4
    You are putting the blob between quotes in your update, which is NOT the proper way to update blobs. You should use bind param to update the blob, see [here](https://stackoverflow.com/questions/8348427/how-to-write-update-oracle-blob-in-a-reliable-way) for an example. – gsalem Jan 05 '20 at 09:35
  • Try printing out the sql so you can run it manually. – Thorbjørn Ravn Andersen Jan 05 '20 at 11:32
  • @gsalem Thank you! With your link, for example, I was able to write a new, correct query. – Hans Jan 05 '20 at 12:42
  • Possible dup of https://stackoverflow.com/questions/8348427/how-to-write-update-oracle-blob-in-a-reliable-way and/or https://stackoverflow.com/questions/8445960/java-reading-blob-from-oracle?rq=1 – Bob Jarvis - Слава Україні Jan 05 '20 at 16:21

0 Answers0