public static String escapeForSQL(String string) {
        if (string == null) {
            return null;
        }
        else if (string.isEmpty()) {
            return string;
        }

        char ch;
        char[] input = string.toCharArray();
        int i = 0;
        int last = 0;
        int len = input.length;
        StringBuilder out = null;
        for (; i < len; i++) {
            ch = input[i];

            if (ch == '\'') {
                if (out == null) {
                     out = new StringBuilder(len + 2);
                }
                if (i > last) {
                    out.append(input, last, i - last);
                }
                last = i + 1;
                out.append('\'').append('\'');
            }
        }

        if (out == null) {
            return string;
        }
        else if (i > last) {
            out.append(input, last, i - last);
        }

        return out.toString();
    }
Escapes all necessary characters in the String so that it can be used in SQL

@param string the string to escape.
@return the string with appropriate characters escaped.
xmppserver/src/main/java/org/jivesoftware/util/StringUtils.java