gpt4 book ai didi

java - 更快地从数据库读取和写入大型 CLOB 文件

转载 作者:行者123 更新时间:2023-12-01 11:44:09 25 4
gpt4 key购买 nike

我的任务是从 Oracle 9i 数据库读取 CLOB 文件并将其写入文件。我的方法适用于小文件,但对于较大的文件(大约 200MB),一个文件需要大约 5 个小时,这是完全 Not Acceptable 。我确信这可以做得更快,但我不知道如何做。这就是我所拥有的:

//get the Clob record from the database and convert it to String
public String getClobRecord() {
Connection conn;
Clob xmlCont;
ResultSet rset;
PreparedStatement stmt;
try {
conn = db.prepareConn();
String sql = "select a.clob_clm from xml_statement a where period_id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, pid);
rset = stmt.executeQuery();
while (rset.next()) {
xmlCont = rset.getClob(1);
xml = ClobStringConversion(xmlCont);
}

stmt.close();
DBConnect.closeConn(conn);
} catch (SQLException asd) {
log.fatal(asd.getMessage());
} catch (IOException asd) {
log.fatal(asd.getMessage());
}
return xml;
}
//My CLOB to String Conversion Method
public static String ClobStringConversion(Clob clb) throws SQLException, IOException {
if (clb == null) {
return "";
}
StringBuilder str = new StringBuilder();
String strng;
BufferedReader br = new BufferedReader(clb.getCharacterStream());
while ((strng = br.readLine()) != null) {
str.append(strng);
}

return str.toString();
}
//Write the String to File
public void writeXmlToFile(String fileDir, String xmlFileName) {
File xmlfile = new File(fileDir + "/" + xmlFileName);
xml = this.getClobRecord();
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(xmlfile));
bw.write(formatXmlFile());
bw.close();
} catch (IOException asd) {
log.fatal(asd.getMessage());
}
}

我到底应该更改什么来容纳非常大的 CLOB 文件?

最佳答案

如果您坚持将 clob 数据存储在内存中,您可以改进的一件事是使用 Oracle 特定的功能。

public static String toString(final Clob clob)
throws SQLException, IOException {

if (clob == null) {
return "";
}

Long length = null;

// try to get the oracle specific CLOB length
// no vendor-specific code here.
try {
final Class<?> oracleClobClass = Class.forName("oracle.sql.CLOB");
if (oracleClobClass.isInstance(clob)) {
length = (Long) oracleClobClass.getMethod("getLength", null)
.invoke(clob, null);
}
} catch (final Exception e) {
}

// we can set initial capacity if we got the length.
final StringBuilder builder
= length == null
? new StringBuilder() : new StringBuilder(length.intValue());

final BufferedReader reader
= new BufferedReader(clob.getCharacterStream());
for (String line = null; (line = reader.readLine()) != null; ) {
builder.append(line);
}

return builder.toString();
}

关于java - 更快地从数据库读取和写入大型 CLOB 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292626/

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