I want to insert something like \mytext
into a MySQL database using JDBC. I tried to use the Java String replace method to find all backslashes and turn them into double-backslashes. In Java itself, I have to escape each backslash by a double-backslash, so in my understanding this should be it:
String sqlProcessed = sqlString.replace("\\", "\\\\")
The resulting string I send to the database using an insert statement, surrounded by 'single quotes' like this:
String query = "INSERT INTO table VALUES ('" + sqlProcessed + "')";
However, instead of showing the expected result, I get a double-backslash in the database, i.e., \\mytext
. On the other hand, without the processing, the backslash is just omitted, i.e., I get mytext
in the database.
What am I doing wrong?