3

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

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:

ORA-01460: unimplemented or unreasonable conversion requested.

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());  
}
6
  • Does it happen when content is null or an empty string? Commented Oct 17, 2011 at 13:12
  • no, if I comment out the 4th param then everything works great. Commented Oct 17, 2011 at 13:30
  • Deleted my answer now that the question has been corrected... is data non-null? Commented Oct 17, 2011 at 13:31
  • I meant: Does it happen when data is null or an empty string? Commented Oct 17, 2011 at 13:32
  • no it will not happen if data is null, it only happens when there is content in the byte[] array Commented Oct 17, 2011 at 13:54

2 Answers 2

5

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

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

InputStream in = new ByteArrayInputStream(data);
pstmt.setBinaryStream(4, in, data.length);
Sign up to request clarification or add additional context in comments.

2 Comments

what would be different on this from using Blob?
@Goodwine: because in my experience getBlob() and setBlob() are either not implemented or don't work properly.
2

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.