java.sql.sqlexception:调用getbytes()中的参数无效

lmvvr0a8  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(322)

我试图用以下代码将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,连接建立正确。我找到了一个类似的答案,但我的答案不起作用!

0pizxfdo

0pizxfdo1#

从javadoc for blob#getbytes:
byte[]getbytes(long pos,int length)引发sqlexception
pos—要提取的blob值中第一个字节的顺序位置;第一个字节位于位置1
所以,你的电话 getBytes() 应该以1作为起始位置,而不是0:

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

相关问题