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?

4
  • 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 for an example. Commented Jan 5, 2020 at 9:35
  • Try printing out the sql so you can run it manually. Commented Jan 5, 2020 at 11:32
  • @gsalem Thank you! With your link, for example, I was able to write a new, correct query. Commented Jan 5, 2020 at 12:42
  • Possible dup of stackoverflow.com/questions/8348427/… and/or stackoverflow.com/questions/8445960/… Commented Jan 5, 2020 at 16:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.