Search code examples
javasqlreplaceoracle12c

Replacing \" in String by " to use in SQL statment


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

Solution

  • You're not using the result of replaceAll.

    Try this:

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

    UPDATE:

    And yes, use replace instead of replaceAll:

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