PreparedStatement 使用like 模糊查询
来源:互联网 发布:外文数据库免费入口 编辑:程序博客网 时间:2024/05/16 18:11
PreparedStatement 使用like
在使用PreparedStatement进行模糊查询的时候废了一番周折,以前一直都没有注意这个问题。一般情况下我们进行精确查询,sql语句类似:select * from table where name =?,然后调用 PreparedStatement的setString等方法给?指定值。那么模糊查询的时候应该怎么写呢?我首先尝试了:select * from customer where name like ‘%?%’。
此时程序报错,因为?被包含在了单引号中,PreparedStatement并不视它为一个参数。后来上网查了相关的一些资料,发现可以这样写select * from table where name like ?;但是在指定参数的时候把?指定为”%”+name+”%”,name是指定的查询条件。这样就OK了。
一般情况下,我总是潜意识的认定了?就是取代所指定的参数,但是实际上我们可以对指定的参数进行了一定的包装之后再传给?,比如这里我们在参数的前后都加了一个%,然后再传给?
String expr = "select * from table where url like ?";
pstmt = con.prepareStatement(expr);
String a="a";
pstmt.setString(1, "%"+a+"%");//自动添加单引号 (包装后的参数)
pstmt.execute();
System.out.println(pstmt.toString());//打印sql
//会默认生成sql: select * from table where url like '%http%'
在使用PreparedStatement进行模糊查询的时候废了一番周折,以前一直都没有注意这个问题。一般情况下我们进行精确查询,sql语句类似:select * from table where name =?,然后调用 PreparedStatement的setString等方法给?指定值。那么模糊查询的时候应该怎么写呢?我首先尝试了:select * from customer where name like ‘%?%’。
此时程序报错,因为?被包含在了单引号中,PreparedStatement并不视它为一个参数。后来上网查了相关的一些资料,发现可以这样写select * from table where name like ?;但是在指定参数的时候把?指定为”%”+name+”%”,name是指定的查询条件。这样就OK了。
一般情况下,我总是潜意识的认定了?就是取代所指定的参数,但是实际上我们可以对指定的参数进行了一定的包装之后再传给?,比如这里我们在参数的前后都加了一个%,然后再传给?
String expr = "select * from table where url like ?";
pstmt = con.prepareStatement(expr);
String a="a";
pstmt.setString(1, "%"+a+"%");//自动添加单引号 (包装后的参数)
pstmt.execute();
System.out.println(pstmt.toString());//打印sql
//会默认生成sql: select * from table where url like '%http%'
阅读全文
0 0
- PreparedStatement 使用like 模糊查询
- PreparedStatement 使用like 模糊查询
- PreparedStatement 使用like 模糊查询
- PreparedStatement 使用like 模糊查询
- PreparedStatement 使用like 模糊查询
- 使用 PreparedStatement 进行模糊查询
- PreparedStatement中使用模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- 使用 PreparedStatement 进行模糊查询
- stm32 中断
- OLAP引擎——Kylin介绍
- 基于BOOST 多线程的日志类
- kmp算法实例讲解
- 巨海集团商学院双十一特惠大礼包,给您一个增值的自己
- PreparedStatement 使用like 模糊查询
- 健康类App原型制作分享-Pillow
- Qt实例-UCF数据标注
- OpenCV3.3-Python3.6.2中KNN使用举例
- JDesktopPane改变窗体大小时背景图片自动适应窗体大小
- Redhat Linux 7.3网络服务无法启动解决办法
- checkbox js验证只有一个可选
- RNG类
- 检查文件是否存在于远程服务器上