在 MySQL 中存储转义字符

storing escape characters in MySQL

我希望能够在 MySQL 数据库中存储一些特殊字符,例如“\r\n”。所以我正在尝试做这样的事情: 插入配置(键,值)值('end.of.line.symbols','\r\n')

然后在我的 Java 代码中,我试图读取此 属性 并将其用于写入文件:

String endOfLineSymbols = "select from config where key = 'end.of.line.symbols'"...
String helloWorld = "Hello, world!";

//writes the specified string to the file + end of the line symbols
writeToFile(helloWorld, endOfLineSymbols);

然后在文件中我看到:

Hello, world!\r\n

所以这些符号不会被识别为换行符,而只是一个简单的字符串。

有什么办法可以解决吗?

谢谢,干杯,安德烈

您可以尝试使用 StringEscapeUtils 如:

String str = StringEscapeUtils.escapeJava(rs.getString("myColumn"));

方法escapeJava():

Escapes the characters in a String using Java String rules.

编辑:

如果你不希望它被转义,那么试试这个:

String str = StringEscapeUtils.unescapeJava(rs.getString("myColumn"));