创建一个方法类,调用该方法类,对接收参数进行转换
public class StringEscapeUtil {
public static String escape(String s) {
StringBuilder sb = new StringBuilder();
//查询字符串一般不会太长,挨个遍历也花费不了多少时间
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// These characters are part of the query syntax and must be escaped
if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')'
|| c == ':' || c == '^' || c == '[' || c == ']' || c == '\"'
|| c == '{' || c == '}' || c == '~' || c == '*' || c == '?'
|| c == '|' || c == '&' || c == ';' || c == '/' || c == '.'
|| c == '$' || c == '%' || c == '_' || Character.isWhitespace(c)) {
sb.append('\\');
}
sb.append(c);
}
return sb.toString();
}
}