java.sql.SQLException:调用中的参数无效:getBytes()
java.sql.SQLException: Invalid argument(s) in call: getBytes()
我尝试使用以下代码将 blob 转换为字符串:
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()));
我的数据库是Oracle,连接正常。我找到了类似的答案,但我的答案不起作用!
来自 Blob#getBytes 的 Javadoc:
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
因此,您对 getBytes()
的调用应该传入 1 作为起始位置,而不是零:
String newValue = new String(newValueBLOB.getBytes(1, (int) newValueBLOB.length()));
作为替代方案,可能更简单,您可以直接使用 ResultSet#getBytes
:
ResultSet rs = stmt.executeQuery(query);
byte[] newValue = rs.getBytes("NEW_VALUE");
String newValueStr = new String(newValue);
我尝试使用以下代码将 blob 转换为字符串:
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()));
我的数据库是Oracle,连接正常。我找到了类似的答案,但我的答案不起作用!
来自 Blob#getBytes 的 Javadoc:
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
因此,您对 getBytes()
的调用应该传入 1 作为起始位置,而不是零:
String newValue = new String(newValueBLOB.getBytes(1, (int) newValueBLOB.length()));
作为替代方案,可能更简单,您可以直接使用 ResultSet#getBytes
:
ResultSet rs = stmt.executeQuery(query);
byte[] newValue = rs.getBytes("NEW_VALUE");
String newValueStr = new String(newValue);