【原创随笔】MySQL查询特殊字符转义

package com.jeecms.cms.dao.assist.impl;

import org.apache.commons.lang.IllegalClassException;

public class SqlEscape {
/**
* 支持String,Integer,Character,Float,Double
*
@param obj
*
@return
*/
public static String escape(Object obj){
if (obj instanceof String||obj instanceof Integer || obj instanceof Character || obj instanceof Float || obj instanceof Double) {
String text = obj.toString();
StringBuffer sbf=new StringBuffer();
for (int i = 0; i < text.toCharArray().length; i++) {
sbf.append("\\").append(text.charAt(i));
}
return sbf.toString();
}else{
throw new IllegalClassException(String.class,obj);
}
}
}

新手,大神勿喷,有好的意见请回复!

你可能感兴趣的:(mysql)