The String.replaceAll(String regex, String replacement) method can be used to escape single quotes in Java.
The correct usage of this method is not quite intuitive. If you construct sql statements in Java, single quotes in a string must be preceded with a backslash(\). However, if you put a single backslash before the quote, the method would still return an unescaped quote. I tried putting a double backslash with the same result. It turns out that the correct number of backslashes is four, two for the regex engine and two for the java compiler:
str = str.replaceAll("’","\\\\’");
The solution was found on the JavaRanch forum.
