Sha*_*min 3 java sql oracle odbc jdbc
我试图使用以下代码将 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()));
Run Code Online (Sandbox Code Playgroud)
我的数据库是 Oracle 并且连接已正确建立。我找到了类似的答案,但我的不起作用!
来自Blob#getBytes的 Javadoc :
byte[] getBytes(long pos, int length) 抛出 SQLException
pos - 要提取的 BLOB 值中第一个字节的序号位置;第一个字节在位置 1
所以,你的调用getBytes()应该传入 1 作为起始位置,而不是零:
String newValue = new String(newValueBLOB.getBytes(1, (int) newValueBLOB.length()));
Run Code Online (Sandbox Code Playgroud)
作为替代方案,可能更简单,您可以直接使用ResultSet#getBytes:
ResultSet rs = stmt.executeQuery(query);
byte[] newValue = rs.getBytes("NEW_VALUE");
String newValueStr = new String(newValue);
Run Code Online (Sandbox Code Playgroud)