我正在尝试使用 java.sql.Clob
方法将 String
数据转换为SubString
(此方法与其他)。 clob数据接近或超过 32MB
。我的观察子字符串方法只能返回 33554342
字节。
如果clob数据超过了33554342个字节,则它会抛出sql异常以下
的 ORA-24817: Unable to allocate the given chunk for current lob operation
EDIT 代码:
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
对象作为对象。我必须转换并从CLOB获取数据
任何想法如何实现这一目标?
答案 0 :(得分:1)
相反:
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。