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

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

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

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

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

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

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

尝试下面的代码,这应该适合你: –

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