首页 > 解决方案 > 将 Blob 保存/读取到 Oracle DB JAVA

问题描述

我想将 Oracle DB 中的图像保存为 BLOB。

尝试执行此操作时,我收到此错误消息

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

我的代码片段:

   ...
   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);
   ...

接下来我尝试了一个新版本,其中我只在数据库中的“oracle.sql.BLOB@”后面插入了十六进制数字(7543e69b)。

那行得通,在读出 blob 后,我又得到了一个 blob 对象。

但将此 blob 对象转换为图标后,无法再显示图像。但是从 blob 转换为图标后,图像不再显示在 GUI 中。并且在调试的时候,图片的大小是,比如width: -1, height: -1, ....

此代码片段:

        ...
        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);

这里出了什么问题?是不是存错了?

标签: javadatabaseoracleblob

解决方案


推荐阅读