最近搞的Hibernate集成oracle817,读取Clob字段时一直报错:
io 异常: 类型长度大于最大值;
百度、google大家都说是驱动问题,换了N个驱动一样的错误,官网oracle817的驱动是.zip的,搞得不知怎么用,总是加载不到驱动,直接解压到classes文件中,报错:
java.lang.AbstractMethodError
最后看到oracle的jdbc目录下有demo,终于找到了正解:
// 读取CLOB字段
static String dumpClob(Connection conn, CLOB clob) throws Exception {
String clobStr = "";
CallableStatement cstmt1 = (CallableStatement) conn
.prepareCall("begin ? := dbms_lob.getLength (?); end;");
CallableStatement cstmt2 = (CallableStatement) conn
.prepareCall("begin dbms_lob.read (?, ?, ?, ?); end;");
cstmt1.registerOutParameter(1, Types.NUMERIC);
cstmt1.setClob(2, clob);
cstmt1.execute();
long length = cstmt1.getLong(1);
long i = 0;
int chunk = 10;
while (i < length) {
cstmt2.setClob(1, clob);
cstmt2.setLong(2, chunk);
cstmt2.registerOutParameter(2, Types.NUMERIC);
cstmt2.setLong(3, i + 1);
cstmt2.registerOutParameter(4, Types.VARCHAR);
cstmt2.execute();
long read_this_time = cstmt2.getLong(2);
String string_this_time = cstmt2.getString(4);
clobStr += string_this_time;
// System.out.print("Read " + read_this_time + " chars: ");
// System.out.println(string_this_time);
i += read_this_time;
}
cstmt1.close();
cstmt2.close();
return clobStr;
}
//读取BOLB
static void fillBlob(Connection conn, BLOB blob, long length)
throws Exception {
CallableStatement cstmt1 = (CallableStatement) conn
.prepareCall("begin dbms_lob.write (?, ?, ?, ?); end;");
long i = 0;
long chunk = 10;
byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
while (i < length) {
cstmt1.setBlob(1, blob);
cstmt1.setLong(2, chunk);
cstmt1.setLong(3, i + 1);
data[0] = (byte) i;
cstmt1.setBytes(4, data);
cstmt1.execute();
i += chunk;
if (length - i < chunk)
chunk = length - i;
}
cstmt1.close();
}
这种问题感觉应该比较常见,在网上竟没有解决方法,特此记录一下,供遇到同样问题的人员参考。