Save text with line breaks in the DB

1

See below a java code to save a SQL command return in PostgreSQL. The command is EXPLAIN ANALYZE. The problem is that by saving this to the database, it loses the line breaks. Someone would know how to help me solve this, that is, so that when doing select and retrieving the text it comes with breaks of lines and indentations. The field I need 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());
    
asked by anonymous 16.01.2018 / 03:50

1 answer

1

After some testing, I'll answer my own question:

The solution was to insert System.getProperty ("line.separator"); at 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);
}
    
16.01.2018 / 04:39