public static String sqlEscapeString(final String str) {
        StringBuilder buffer = new StringBuilder(str.length() * 2);
        boolean neededEscaping = false;
        for (int i = 0; i < str.length(); i++) {
            final char c = str.charAt(i);
            if (needsEscaping((byte) c)) {
                neededEscaping = true;
                buffer.append('\\');
            }
            buffer.append(c);
        }
        return neededEscaping ? buffer.toString() : str;
    }
public static String sqlEscapeString ( final String str ) { StringBuilder buffer = new StringBuilder ( str . length ( ) * 2 ) ; boolean neededEscaping = false ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { final char c = str . charAt ( i ) ; if ( needsEscaping ( ( byte ) c ) ) { neededEscaping = true ; buffer . append ( '\\' ) ; } buffer . append ( c ) ; } return neededEscaping ? buffer . toString ( ) : str ; }
escapes the given string, new string length is at most twice the length of str

@param str the string to escape
@return an escaped string