gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:56:02 25 4
gpt4 key购买 nike

我正在尝试使用以下代码将 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);

关于java.sql.SQLException : Invalid argument(s) in call: getBytes() 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62493953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com