Save text with line breaks in BD

Asked

Viewed 76 times

0

See below for a java code to save an SQL command return in Postgresql. The command is EXPLAIN ANALYZE. The problem is that while saving this in the database, it loses line breaks. Someone would know how to help me solve this, that is, so that when selecting and recovering the text it comes with line breaks and indentations. The field that I need from this is the FULL_EXPLAIN below, and its data type is the "text" of postgresql.

---------------- 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 answer

0

After a few tests, I’ll answer my own question:

The solution was to insert System.getProperty("line.separator"); in each iteration of the While loop, see:

String variavel = System.getProperty("line.separator");
 SQLtxtQuery2 = SQLtxtQuery;
 while (result2.next()) {
    SQLtxtQuery2 = result2.getString(1);
    str1.append(SQLtxtQuery2);
    str1.append(variavel);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.