java中对SQL模糊查询通配符%的处理
2012-07-01 15:08
441 查看
在模糊查询的SQL语句中,如果有用户输入查询通配符‘%’,使用 select * from table where code like '%condition%'的SQL,会查出全部记录,这个如何解决叱?
EscapeUtils的escapeStr方法:
其实就是利用oracle的escape函数进行转义,把通配符转义成普通符号使用。
if(!StringUtils.isEmpty(_cname)){ /** 处理模糊通配符%和_ */ sql.append(" and c.FCOURSEWARE_NAME LIKE '%").append(EscapeUtils.escapeStr(_cname)).append("%' escape '\\'"); model.addAttribute("_cname", _cname); }
EscapeUtils的escapeStr方法:
/** * Description: 处理转义字符%和_,针对ORACLE数据库 * @param str * @return */ 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; }
其实就是利用oracle的escape函数进行转义,把通配符转义成普通符号使用。
/** 总行数 */ String _sql = "select count(1) from ("+sql.toString()+") "; long rowCount; if(!StringUtils.isEmpty(keyWords)){ rowCount = courseInfoService.getCountByJdbc(_sql, map); }
相关文章推荐
- sql语句中模糊查询Like中通配符的处理
- 动态SQL— —模糊查询语句(存储过程)中关于百分号%的处理
- oracle sql语言模糊查询--通配符like的使用教程
- T-SQL---多值模糊查询的处理
- 动态SQL模糊查询语句(存储过程)中关于百分号%的处理
- sql 模糊查询like 如何处理 NULL
- 基于hibernate的基本dao接口与实现 及 sql模糊查询特殊字符处理 及 struts 特殊字符在界面正常回显
- SQL查询语句通配符与ACCESS模糊查询like的解决方法
- SQL查询语句通配符与ACCESS模糊查询like的解决方法
- java处理模糊查询转义“%_”
- sql 查询 下划线 _ 克服通配符,模糊查询通配符字符
- SQL Server之 (二) SQL语句 模糊查询 空值处理 聚合函数
- 如何在SQL模糊查询中忽略对大小写字符的处理
- oracle sql语言模糊查询--通配符like的使用教程
- T-SQL---多值模糊查询的处理
- Orcale对DBUtils不支持模糊查询(Exception:java.sql.SQLException: ORA-00904)的原因与解决
- sql特殊字符的模糊查询处理
- mybatis 模糊查询报错: java.sql.SQLException: ORA-01006: 绑定变量不存在
- 模糊查询/空值处理/数据排序oreder by/数据分组group by/SQL语句执行顺序
- SQL模糊查询,使用like比较字,加上SQL里的通配符