在 Oracle 数据库中将 byte[] 数组作为 blob 插入获取 ORA-01460:请求未实现或不合理的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7794197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:22:49  来源:igfitidea点击:

Inserting byte[] array as blob in Oracle Database getting ORA-01460: unimplemented or unreasonable conversion requested

javaoraclejava-stored-procedures

提问by medium

I have a java stored procedure that I am trying to insert a byte[] array into an oracle blob field in a table.

我有一个 java 存储过程,我试图将一个 byte[] 数组插入到表中的 oracle blob 字段中。

I create a prepared statement as follows but it will randomly fail when I execute the prepared statement. I have narrowed down that the issue is coming from the pstmt.setBytes(4,content). The error I get is:

我按如下方式创建了一个准备好的语句,但是当我执行准备好的语句时它会随机失败。我已经缩小了问题来自 pstmt.setBytes(4,content) 的范围。我得到的错误是:

ORA-01460: unimplemented or unreasonable conversion requested.

ORA-01460: 请求的转换未实现或不合理。

private static void insertFile(Connection connOracle, int zipFileId, byte[] data, String filepath, String filename ) throws SQLException {

try {
    String QUERY = "INSERT INTO files(file_id, zip_file_id, filename, file_path, content) VALUES(SEQ_FILE_ID.nextval,?,?,?,?)";

    PreparedStatement pstmt = connOracle.prepareStatement(QUERY);

    pstmt.setInt(1,zipFileId);
    pstmt.setString(2, filename);
    pstmt.setString(3, filepath);
    pstmt.setBytes(4, data);

    System.out.println("INSERTING file_id " + filepath + ", " + filename + " INTO DATABASE");

    pstmt.execute();
    pstmt.close();
}
catch (SQLException e) {
    throw new SQLException(e.getMessage());  
}

回答by a_horse_with_no_name

If I recall correctly the Oracle JDBC drivers (at least older ones - you didn't tell us which version you are using) don't support setBytes()(or getBytes()).

如果我没记错的话,Oracle JDBC 驱动程序(至少是旧的 - 您没有告诉我们您使用的是哪个版本)不支持setBytes()(或getBytes())。

In my experience, using setBinaryStream()is much more reliable and stable:

根据我的经验,使用setBinaryStream()更可靠和稳定:

InputStream in = new ByteArrayInputStream(data);
pstmt.setBinarySream(4, in, data.length);

回答by M.J.

try with the below code, this should work for you :-

尝试使用以下代码,这应该适合您:-

Blob blobValue = new SerialBlob(data);
pstmt.setBlob(4, blobValue);