【问题标题】:trying to insert image into database getting ORA-01460: unimplemented or unreasonable conversion requested error试图将图像插入数据库得到 ORA-01460:未实现或不合理的转换请求错误
【发布时间】:2023-03-29 17:27:01
【问题描述】:

我正在尝试将图像插入数据库并使用自动增量 photo_id。

我的表格列是:

PHOTO_ID  (primary key),
USERNAME  (fkey),
PICTURE,
PICTURE_TITLE,
LIKES 

我的代码是

public class UploadPhotoDao {
    public int insertPhoto(UploadPhotoBean uploadBean){
        Connection con = null;
        PreparedStatement ps = null;

        int index = 0;
        final String query = "INSERT INTO PHOTOS_DETAILS (PHOTO_ID,USERNAME,PICTURE,PICTURE_TITLE,LIKES) VALUES (PHOTO_ID_SEQUENCE.nextval,?,?,?,?)";
            try {
                con = ConnectionUtil.getConnection();
                ps = con.prepareStatement(query);


                ps.setString(1, uploadBean.getUsername());
                File file =new File(uploadBean.getPicture());
                FileInputStream fis = new FileInputStream(file);
                ps.setBinaryStream(2, fis, fis.available());
                ps.setString(3, uploadBean.getPictureTitle());
                ps.setInt(4, new Integer(0));
                index = ps.executeUpdate();

            }
            catch (SQLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            finally {
                    ConnectionUtil.closeQuitly(ps);
                    ConnectionUtil.closeQuitly(con);
            }
            return index;
    }
}

我收到了错误:

java.sql.SQLException: ORA-01460: 请求未实现或不合理的转换

【问题讨论】:

  • 如果省略 BLOB 列,插入语句是否有效?如果为 BLOB 列提供 NULL 是否有效?
  • 解决了这个问题..只是一个 ojdbc14.jar 文件问题...需要获取最新版本。就是这样

标签: java database image jsp insert


【解决方案1】:

InputStream.available() 确实返回流的大小(Javadocs 中清楚地记录了这一点)。

您需要查询文件的大小,而不是输入流中的“可用字节”:

ps.setBinaryStream(2, fis, (int)file.length());

根据您的 JDBC 驱动程序版本,您也可以使用

ps.setBinaryStream(2, fis, file.length());

setBinaryStream(int, InputStream, long) 是在 JDBC 4.0 中定义的,因此并非由所有驱动程序版本实现。 setBinaryStream(int, InputStream, int) 是 JDBC 2.0(如果我没记错的话),应该在所有版本中都可用。

【讨论】:

  • 即使我更改为 ps.setBinaryStream(2, fis, (int)file.length()); 也会出现相同的错误请检查我的查询...是否有任何错误???我在 db 表中将我的图片列声明为 BLOB。并且我使用一个序列来自动增加照片 ID 列.....
  • @user1900771:对不起,那我没有更多的想法了
  • 嗨...我解决了这个问题...只是驱动问题..将 ojdbc14.jar 更改为最新版本。
猜你喜欢
  • 1970-01-01
  • 2011-12-29
  • 2020-06-28
  • 2012-06-17
  • 2015-10-14
  • 2011-12-09
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多