0

I'm handling user data and store it to oracle which may contain "'", "''", or "'''". I have try to use replaceAll() method to convert data but it output not my expected result.

try replaceAll() but not work

String sAddress1="";
sAddress1 = "ABC''S ROA'''D";

sAddress1 = sAddress1.replaceAll("'","''");

I expect the output of sAddress1 to be:

"ABC''''S ROA''''''D"

But the actual output is:

"ABC''S ROA''''D"

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
stockjoe
  • 1
  • 1

1 Answers1

0

Your code works correctly. The problem is the persistence in your Oracle DB.

In which way are you storing it into the DB? Are you using native SQL? Are you using JPA/Hibernate?

Probably you are using a Native SQL, since the JPA/Hibernate options should handle the quoting for you.

Take a look to the text literials section in the Oracle documentation https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm#sthref344

Or take a look to other answers about escaping single quoutes for Oracle DB PL/SQL, how to escape single quote in a string? Escaping single quote in PLSQL

Ezequiel
  • 3,477
  • 1
  • 20
  • 28
  • Even pure JDBC usage would have options for doing this for you. And technically, Hibernate nor JPA will quote for you, but use prepared statements with parameters. – Mark Rotteveel Aug 05 '19 at 09:00