public static String escape(final String text, final boolean forLike) {
        if (text == null) {
            return "";
        }
        String escapedText = text.replaceAll("'", "''");
        if (forLike) {
            escapedText = escapedText.replaceAll("_", "\\\\_")
                    .replaceAll("%", "\\\\%")
                    .replaceAll("\\\\", "\\\\\\\\");
        }
        return escapedText;
    }
Helper method for escaping text when building SQL
main/src/main/java/cgeo/geocaching/storage/SqlBuilder.java