在文本字段

时间:2018-01-15 17:17:06

标签: java postgresql text

请参阅用于在数据库PostgreSQL中保存执行计划的Java代码,问题是此计划保存在数据库中,丢失了换行符和缩进。如果有人可以帮我制作这段代码,请保留缩写和换行符,这些缩进和换行符来自PostgreSQL返回Explain Analyze命令。问题的领域是FULL_EXPLAIN,这个字段是postgresql表中的Text类型。

---------------- JAVA CODE ---------------------

StringBuilder ConsultaExplain = new StringBuilder();
ConsultaExplain.append("EXPLAIN (ANALYZE, BUFFERS)  ");
ConsultaExplain.append(SQLtxtQuery);

ResultSet result2 = BancoDeDados.getResultSet(ConsultaExplain.toString());

    StringBuilder str1 = new StringBuilder();
    str1.append("Execution plan...:");
    SQLtxtQuery2 = SQLtxtQuery;
    while (result2.next()) {
        SQLtxtQuery2 = result2.getString(1);
        str1.append(SQLtxtQuery2);
    }
        String myString = str1.toString();
        String myNewStringNoQuotes = myString.replaceAll("'", "_");

    StringBuilder exeSQLUpdateTabAcao = new StringBuilder();
    exeSQLUpdateTabAcao.append(
    "UPDATE TAB_CTRL_TEMPO SET OCORRENCIA_TEMPO= current_timestamp, FULL_EXPLAIN ='");
    exeSQLUpdateTabAcao.append(myNewStringNoQuotes);
    exeSQLUpdateTabAcao.append("' WHERE id = currval('tab_ctrl_tempo_id_seq')");
    BancoDeDados.execStatement(exeSQLUpdateTabAcao.toString());

1 个答案:

答案 0 :(得分:0)

您的代码通常看起来没问题,但是您将所有结果行打包成一个字符串而没有换行符。 尝试在处理结果的循环中添加换行符,请参见下文:

StringBuilder ConsultaExplain = new StringBuilder();
ConsultaExplain.append("EXPLAIN (ANALYZE, BUFFERS)  ");
ConsultaExplain.append(SQLtxtQuery);

ResultSet result2 = BancoDeDados.getResultSet(ConsultaExplain.toString());

    StringBuilder str1 = new StringBuilder();
    str1.append("Execution plan...:");
    SQLtxtQuery2 = SQLtxtQuery;
    while (result2.next()) {
        SQLtxtQuery2 = result2.getString(1);
        str1.append(SQLtxtQuery2);
        str1.append("E'\\\\r\\\\n'");              // INSERT LINE BREAK AFTER EACH RECORD
    }
        String myString = str1.toString();
        String myNewStringNoQuotes = myString.replaceAll("'", "_");

    StringBuilder exeSQLUpdateTabAcao = new StringBuilder();
    exeSQLUpdateTabAcao.append(
    "UPDATE TAB_CTRL_TEMPO SET OCORRENCIA_TEMPO= current_timestamp, FULL_EXPLAIN ='");
    exeSQLUpdateTabAcao.append(myNewStringNoQuotes);
    exeSQLUpdateTabAcao.append("' WHERE id = currval('tab_ctrl_tempo_id_seq')");
    BancoDeDados.execStatement(exeSQLUpdateTabAcao.toString());