比如输入%,那么就变为 Like "%", 需要Escape,怎么Escape哇?mysql…… 都有哪些字符需要Escape哇? ------------------------------------ 把检索用的关键字作如下转换 将% 转化成 /% 将/ 转化成// 将_ 转化成/_ 在执行 LIKE ? Escape '/' 就可以了。 ------------------------------------- 比如输入%,那么就变为 Like "%", 的话
\% Matches one “%” character \_ Matches one “_” character 就可以了 \n⇒\\n \⇒\\\\ --------------------------------------------- Java code:
public static String escapeForSql(Object basic){ String strBasic = null; if(basic ==null){ strBasic = ""; }else{ strBasic = String.valueOf(basic); if(0<strBasic.length()){ if(strBasic.indexOf("\\")!=-1){ strBasic = strBasic.replace("\\","\\\\"); //"\" --> "\\" } strBasic = strBasic.replaceAll("'","\\\\'"); //"'" --> "\'" strBasic = strBasic.replaceAll("_","\\\\_"); //"_" --> "\_" strBasic = strBasic.replaceAll("%","\\\\%"); //"%" --> "\%" } } return strBasic; }
|