Andrey Yaskulsky
Andrey Yaskulsky

Reputation: 2516

storing escape characters in MySQL

I want to be able to store some special characters like "\r\n" in MySQL database. So I'm trying to do something like that: insert into config( key, value) values('end.of.line.symbols', '\r\n')

Then in my Java code I'm trying to read this property and use it for writing in file:

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);

Then in the file I see:

Hello, world!\r\n

So these symbols are not recognised as a line break but just as a simple string.

Is it possible to resolve it somehow?

Thanks, cheers, Andrey

Upvotes: 1

Views: 2562

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

You can try to use StringEscapeUtils like:

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

The method escapeJava():

Escapes the characters in a String using Java String rules.

EDIT:

If you dont want it to be escaped then try this:

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

Upvotes: 4

Related Questions