我正在尝试使用java.sql.Clob
方法将String
数据转换为SubString
(与其他方法相比,该方法具有良好的性能)。 Clob数据的值接近或大于32MB
。 AS我的观察子字符串方法只能返回最多33554342
个字节。
如果clob数据跨越33554342字节,则将其抛出sql异常以下ORA-24817: Unable to allocate the given chunk for current lob operation
编辑
码:
public static void main(String[] args) throws SQLException {
Main main = new Main();
Connection con = main.getConnection();
if (con == null) {
return;
}
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT Table_ID,CLOB_FILE FROM TableName WHERE SOMECONDITION ";
String table_Id = null;
String directClobInStr = null;
CLOB clobObj = null;
String clobStr = null;
Object obj= null;
try {
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
table_Id = rs.getString( "Table_ID" ) ;
directClobInStr = rs.getString( "clob_FILE" ) ;
obj = rs.getObject( "CLOB_FILE");
clobObj = (CLOB) obj;
System.out.println("Table id " + table_Id);
System.out.println("directClobInStr " + directClobInStr);
clobStr = clobObj.getSubString(1L, (int)clobObj.length() );//33554342
System.out.println("clobDataStr = " + clobStr);
}
}
catch (SQLException e) {
e.printStackTrace();
return;
}
catch (Exception e) {
e.printStackTrace();
return;
}
finally {
try {
rs.close();
pstmt.close();
con.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
注意:-在这里
obj = rs.getObject( "CLOB_FILE");
工作,但是我不期望这样。因为我从某处获取ResultSet
对象作为Object。我必须转换并从CLOB获取数据任何想法如何实现这一目标?
最佳答案
代替:
clobStr = clobObj.getSubString(1L, (int)clobObj.length() );
尝试类似的方法:
int toread = (int) clobObj.length();
int read = 0;
final int block_size = 8*1024*1024;
StringBuilder str = new StringBuilder(toread);
while (toread > 0) {
int current_block = Math.min(toread, block_size);
str.append(clobObj.getSubString(read+1, current_block));
read += current_block;
toread -= current_block;
}
clobStr = str.toString();
它使用循环提取子字符串(每次迭代8MB)。
但是请记住,据我所知,Java字符串限制为2 GB(这就是将read声明为int而不是long的原因),而Oracle CLOB限制为128 TB。