0

So I'm currently working on a small life helper that transforms an ARX document (migration document in the ARS worl) into an SQL statement I can check on the DB.

An ARX is a basic document which holds information like the table name, the column name and the value names of new entries. I'm trying to read those ARX with java in order to create SQL statements that can check if the entries were really created ont he ARS server.

Problem seems to be the differences between Java string syntax (" for strings etc.) and SQL syntax (' for strings). So I'm working on replacing those Java chars with the SQL ones. I'm trying to do this for \" too, because in Java that works, but in SQL \" is just a normal string with no special meaning.

Where was my mistake? Is there a easier way to do this? I can't be the first person in the world to try to do this.

String a = "123\\\"123\\\"123";
System.out.println(a);
a.replaceAll("\\\"", "\"");
System.out.println(a);

Expected result:

123\"123\"123
123"123"123

Real result:

123\"123\"123
123\"123\"123
Ruben
  • 3
  • 1
  • 1
    `"123\\\"123\\\"123"` <-- this generally makes no sense. Why would your source string be escaping a double quote (which does nothing) ? – Tim Biegeleisen Feb 19 '19 at 07:52
  • 2
    Not sure if this is applicable to your situation, but have you looked into prepared statements? Generally speaking the database driver will take care of encoding issues for you: https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html – Jeroen Steenbeeke Feb 19 '19 at 07:52
  • 1
    Unless you're trying to use regular expressions, you should use `a.replace`, not `a.replaceAll`. `replaceAll` tries to interpret its argument as a regular expression, which uses backslashes for its own purposes. – khelwood Feb 19 '19 at 07:55
  • 1
    Don't do this at all--as Jeroen recommended, use a prepared statement instead, which removes any need to do escaping. – chrylis -cautiouslyoptimistic- Feb 19 '19 at 08:00

2 Answers2

0

You're not using the result of replaceAll.

Try this:

a = a.replaceAll("\\\"", "\"");

UPDATE:

And yes, use replace instead of replaceAll:

a = a.replace("\\\"", "\"");
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

You should not use the replaceAll method under your condition.

here the code below for you reference.

 String a = "123\\\"123\\\"123";
 System.out.println(a);
 a = a.replace("\\", "");
 System.out.println(a);
wenchunl
  • 21
  • 1