试图将图像插入数据库得到 ORA-01460:未实现或不合理的转换请求错误

2022-11-12Java开发问题
41

本文介绍了试图将图像插入数据库得到 ORA-01460:未实现或不合理的转换请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

我的表格列是:

PHOTO_ID(主键),用户名 (fkey),图片,PICTURE_TITLE,喜欢

我的代码是

公共类 UploadPhotoDao {公共 int insertPhoto(UploadPhotoBean uploadBean){连接 con = null;PreparedStatement ps = null;整数索引 = 0;最终字符串查询=插入照片详细信息(PHOTO_ID,用户名,图片,图片标题,喜欢)值(PHOTO_ID_SEQUENCE.nextval,?,?,?,?)";尝试 {con = ConnectionUtil.getConnection();ps = con.prepareStatement(查询);ps.setString(1, uploadBean.getUsername());文件文件 =new File(uploadBean.getPicture());FileInputStream fis = new FileInputStream(file);ps.setBinaryStream(2, fis, fis.available());ps.setString(3, uploadBean.getPictureTitle());ps.setInt(4, 新整数(0));index = ps.executeUpdate();}捕捉(SQLException e){e.printStackTrace();} 捕捉(FileNotFoundException e){//TODO 自动生成的 catch 块e.printStackTrace();} 捕捉(IOException e){//TODO 自动生成的 catch 块e.printStackTrace();}最后 {ConnectionUtil.closeQuitly(ps);ConnectionUtil.closeQuitly(con);}回报指数;}}

我收到了错误:

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

解决方案

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(如果我没记错的话),应该在所有版本中都可用.

I'm trying to insert an image into database and with auto increment photo_id.

my table columns are:

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

my code is

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;
    }
}

I'm getting the error:

java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested

解决方案

InputStream.available() does not return the size of the stream (which is clearly documented in the Javadocs).

You will need to query the size of the file, not the "available bytes" in the inputstream:

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

depending on the version of your JDBC driver you can also use

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

But setBinaryStream(int, InputStream, long) is defined in JDBC 4.0 and thus not implemented by all driver versions. setBinaryStream(int, InputStream, int) is JDBC 2.0 (if I'm not mistaken) and should be available in all versions.

这篇关于试图将图像插入数据库得到 ORA-01460:未实现或不合理的转换请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13