`

java处理oracle中转义字符%和_

阅读更多

 一般针对输入框查询,后台做的是模糊查询,那么针对输入框中有特殊字符%或者_时,应如何查询?

/**
 * @类功能说明:处理转义字符%和_,针对ORACLE数据库
 * @创建日期:2013-8-21
 * @版本:V1.0
 */
public class EscapeUtils {
	public static String escapeStr(String str){
		if(str.startsWith("%") || str.startsWith("_")){
			str = "\\" + str;
		}
		
		if(str.endsWith("_")){
			int index = str.indexOf("_");
			str = str.substring(0, index) + "\\" + "_";
		}
		
		if(str.endsWith("%")){
			int index = str.indexOf("%");
			str = str.substring(0, index) + "\\" + "%";
		}
		
		return str;
	}
	
	public static void main(String[] args) {
		String queryCondition = null;
		//演示使用
		StringBuffer sb = new StringBuffer();
		if(StrUtil.isNotNull(queryCondition)){
			/**  处理模糊通配符%和_  */
			sb.append("and s.name like '%").append(EscapeUtils.escapeStr(queryCondition)).append("%' escape '\\'");
		}
	}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics