java.sql.SQLException: Invalid argument(s) in call: getBytes()

Viewed 1090

I was trying to convert a blob to string with the following code:

ResultSet rs = stmt.executeQuery(query);

Blob newValueBLOB = rs.getBlob("NEW_VALUE");
System.out.println(newValueBLOB);
String newValue = new String(newValueBLOB.getBytes(0, (int) newValueBLOB.length()));

My database is Oracle and the connection is established properly. I've found a similar answer but mine is not working!

1 Answers

From the Javadoc for Blob#getBytes:

byte[] getBytes(long pos, int length) throws SQLException
pos - the ordinal position of the first byte in the BLOB value to be extracted; the first byte is at position 1

So, your call to getBytes() should be passing in 1 as the start position, not zero:

String newValue = new String(newValueBLOB.getBytes(1, (int) newValueBLOB.length()));

As an alternative, possibly simpler, you could just use ResultSet#getBytes directly:

ResultSet rs = stmt.executeQuery(query);
byte[] newValue = rs.getBytes("NEW_VALUE");
String newValueStr = new String(newValue);
Related