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

时间:2015-03-27 03:05:57

标签: java jdbc oracle9i clob

我有一项任务是从Oracle 9i数据库中读取CLOB文件并将其写入文件。我对小文件有用,但在文件较大的情况下,大约200MB这对于一个文件需要大约5个小时,这是完全不可接受的。我相信这可以更快完成,但我不知道如何。这就是我所拥有的:

//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文件?

1 个答案:

答案 0 :(得分:1)

如果你坚持将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();
}