1:%放在占位符中
              parameters.add("%"+familyMemberQueryBean.getFullName()+"%");
              sql+=" and t.full_name like ?";

2:使用concat函数
                parameters.add(familyMemberQueryBean.getFullName());
                sql+=" and t.full_name like concat('%',?,'%')";

3:使用转义字符\,百分号直接写在sql语句中
                parameters.add(familyMemberQueryBean.getFullName());
                sql+=" and t.full_name like \"%\"?\"%\"";

直接查询的SQL语句如下:SELECT id,full_name,email,phone,remark FROM family_member t WHERE 1 = 1
                                                AND t.full_name LIKE "%"'李'"%" AND t.email LIKE "%"'qq'"%"

4:直接拼接SQL
                sql+=" and t.full_name like '%"+familyMemberQueryBean.getFullName()+"%'";

例如:

 package org.pine.dao.impl;

 import org.pine.dao.FamilyMemberDao;
import org.pine.dto.FamilyMember;
import org.pine.util.DbcpUtils;
import org.pine.vo.FamilyMemberQueryBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List; /**
* FamilyMemberDao实现类
*
* @Auther: zts
* @Date: 2019/5/28 下午 03:21
* @version: 1.0
*/
public class FamilyMemberDaoImpl implements FamilyMemberDao {
/*
public List<FamilyMember> queryFamilyMembers(FamilyMemberQueryBean familyMemberQueryBean) {
Connection connection =null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection = DbcpUtils.getConnection();
String sql ="SELECT id,full_name,email,phone,remark FROM family_member t WHERE 1 = 1 ";
//拼接SQL
List<Object> parameters = new ArrayList<Object>();
if(familyMemberQueryBean.getFullName()!=null&&familyMemberQueryBean.getFullName().length()>0){
parameters.add("%"+familyMemberQueryBean.getFullName()+"%");
sql+=" and t.full_name like ?";
}
if(familyMemberQueryBean.getEmail()!=null&&familyMemberQueryBean.getEmail().length()>0){
parameters.add("%"+familyMemberQueryBean.getEmail()+"%");
sql+=" and t.email like ?";
}
if(familyMemberQueryBean.getPhone()!=null&&familyMemberQueryBean.getPhone().length()>0){
parameters.add("%"+familyMemberQueryBean.getPhone()+"%");
sql+=" and t.phone like ?";
}
if(familyMemberQueryBean.getRemark()!=null&&familyMemberQueryBean.getRemark().length()>0){
parameters.add("%"+familyMemberQueryBean.getRemark()+"%");
sql+=" and t.remark like ?";
}
sql+="limit ?,?";
parameters.add(familyMemberQueryBean.getStart());
parameters.add(familyMemberQueryBean.getLimit());
DbcpUtils.printlnSQL(sql);
DbcpUtils.printlnParameters(parameters);
preparedStatement = connection.prepareStatement(sql);
//设置参数
for(int i=0;i<parameters.size();i++){
preparedStatement.setObject(i+1,parameters.get(i));
}
resultSet = preparedStatement.executeQuery();
List<FamilyMember> familyMembers = new ArrayList<FamilyMember>();
while (resultSet.next()){
FamilyMember familyMember = new FamilyMember();
int id = resultSet.getInt("id");
String fullName = resultSet.getString("full_name");
String email = resultSet.getString("email");
String phone = resultSet.getString("phone");
String remark = resultSet.getString("remark");
familyMember.setId(id);
familyMember.setFullName(fullName);
familyMember.setEmail(email);
familyMember.setPhone(phone);
familyMember.setRemark(remark);
familyMembers.add(familyMember);
}
return familyMembers;
}catch (Exception e){
throw new RuntimeException(e);
}finally {
DbcpUtils.closeConnection(connection,preparedStatement,resultSet);
}
}*/ /*
@Override
public int queryFamilyMemberCount(FamilyMemberQueryBean familyMemberQueryBean) {
Connection connection =null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection = DbcpUtils.getConnection();
String sql ="SELECT count(*) FROM family_member t WHERE 1 = 1 ";
//拼接SQL
List<String> parameters = new ArrayList<String>();
if(familyMemberQueryBean.getFullName()!=null&&familyMemberQueryBean.getFullName().length()>0){
parameters.add(familyMemberQueryBean.getFullName());
sql+=" and t.full_name like concat('%',?,'%')";
}
if(familyMemberQueryBean.getEmail()!=null&&familyMemberQueryBean.getEmail().length()>0){
parameters.add(familyMemberQueryBean.getEmail());
sql+=" and t.email like concat('%',?,'%')";
}
if(familyMemberQueryBean.getPhone()!=null&&familyMemberQueryBean.getPhone().length()>0){
parameters.add(familyMemberQueryBean.getPhone());
sql+=" and t.phone like concat('%',?,'%')";
}
if(familyMemberQueryBean.getRemark()!=null&&familyMemberQueryBean.getRemark().length()>0){
parameters.add(familyMemberQueryBean.getRemark());
sql+=" and t.remark like concat('%',?,'%')";
}
DbcpUtils.printlnSQL(sql);
DbcpUtils.printlnParameters(parameters);
preparedStatement = connection.prepareStatement(sql);
//设置参数
for(int i=0;i<parameters.size();i++){
preparedStatement.setString(i+1,parameters.get(i));
}
resultSet = preparedStatement.executeQuery();
resultSet.next();
int count = resultSet.getInt(1);
return count;
}catch (Exception e){
throw new RuntimeException(e);
}finally {
DbcpUtils.closeConnection(connection,preparedStatement,resultSet);
}
}*/ public List<FamilyMember> queryFamilyMembers(FamilyMemberQueryBean familyMemberQueryBean) {
Connection connection =null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection = DbcpUtils.getConnection();
String sql ="SELECT id,full_name,email,phone,remark FROM family_member t WHERE 1 = 1 ";
//拼接SQL
List<Object> parameters = new ArrayList<Object>();
if(familyMemberQueryBean.getFullName()!=null&&familyMemberQueryBean.getFullName().length()>0){
parameters.add(familyMemberQueryBean.getFullName());
sql+=" and t.full_name like \"%\"?\"%\"";
}
if(familyMemberQueryBean.getEmail()!=null&&familyMemberQueryBean.getEmail().length()>0){
parameters.add(familyMemberQueryBean.getEmail());
sql+=" and t.email like \"%\"?\"%\"";
}
if(familyMemberQueryBean.getPhone()!=null&&familyMemberQueryBean.getPhone().length()>0){
parameters.add(familyMemberQueryBean.getPhone());
sql+=" and t.phone like \"%\"?\"%\"";
}
if(familyMemberQueryBean.getRemark()!=null&&familyMemberQueryBean.getRemark().length()>0){
parameters.add(familyMemberQueryBean.getRemark());
sql+=" and t.remark like \"%\"?\"%\"";
}
sql+=" limit ?,?";
parameters.add(familyMemberQueryBean.getStart());
parameters.add(familyMemberQueryBean.getLimit());
DbcpUtils.printlnSQL(sql);
DbcpUtils.printlnParameters(parameters);
preparedStatement = connection.prepareStatement(sql);
//设置参数
for(int i=0;i<parameters.size();i++){
preparedStatement.setObject(i+1,parameters.get(i));
}
resultSet = preparedStatement.executeQuery();
List<FamilyMember> familyMembers = new ArrayList<FamilyMember>();
while (resultSet.next()){
FamilyMember familyMember = new FamilyMember();
int id = resultSet.getInt("id");
String fullName = resultSet.getString("full_name");
String email = resultSet.getString("email");
String phone = resultSet.getString("phone");
String remark = resultSet.getString("remark");
familyMember.setId(id);
familyMember.setFullName(fullName);
familyMember.setEmail(email);
familyMember.setPhone(phone);
familyMember.setRemark(remark);
familyMembers.add(familyMember);
}
return familyMembers;
}catch (Exception e){
throw new RuntimeException(e);
}finally {
DbcpUtils.closeConnection(connection,preparedStatement,resultSet);
}
} @Override
public int queryFamilyMemberCount(FamilyMemberQueryBean familyMemberQueryBean) {
Connection connection =null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection = DbcpUtils.getConnection();
String sql ="SELECT count(*) FROM family_member t WHERE 1 = 1 ";
//拼接SQL
List<String> parameters = new ArrayList<String>();
if(familyMemberQueryBean.getFullName()!=null&&familyMemberQueryBean.getFullName().length()>0){
sql+=" and t.full_name like '%"+familyMemberQueryBean.getFullName()+"%'";
}
if(familyMemberQueryBean.getEmail()!=null&&familyMemberQueryBean.getEmail().length()>0){
sql+=" and t.email like '%"+familyMemberQueryBean.getEmail()+"%'";
}
if(familyMemberQueryBean.getPhone()!=null&&familyMemberQueryBean.getPhone().length()>0){
sql+=" and t.phone like '%"+familyMemberQueryBean.getPhone()+"%'";
}
if(familyMemberQueryBean.getRemark()!=null&&familyMemberQueryBean.getRemark().length()>0){
sql+=" and t.remark like '"+familyMemberQueryBean.getRemark()+"'";
}
DbcpUtils.printlnSQL(sql);
DbcpUtils.printlnParameters(parameters);
preparedStatement = connection.prepareStatement(sql);
//设置参数
for(int i=0;i<parameters.size();i++){
preparedStatement.setString(i+1,parameters.get(i));
}
resultSet = preparedStatement.executeQuery();
resultSet.next();
int count = resultSet.getInt(1);
return count;
}catch (Exception e){
throw new RuntimeException(e);
}finally {
DbcpUtils.closeConnection(connection,preparedStatement,resultSet);
}
}
}

JDBC模糊查询的4种方式的更多相关文章

  1. MyBatis实现模糊查询的几种方式

    在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: ...

  2. 【mysql模糊查询的几种方式】

    select * from activyty_code where acname like '%yj%' 1:%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分 ...

  3. mysql中模糊查询的四种用法介绍

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

  4. JDBC注册驱动的三种方式

    JDBC注册驱动的三种方式 1.通过导入的JDBC的驱动包拿到的com.mysql.jdbc.Driver对象,利用java.sql.DriverManager对象的DriverManager.reg ...

  5. mysql进阶(六)模糊查询的四种用法介绍

    mysql中模糊查询的四种用法介绍 这篇文章主要介绍了mysql中模糊查询的四种用法,需要的朋友可以参考下. 下面介绍mysql中模糊查询的四种用法: 1 %: 表示任意0个或多个字符.可匹配任意类型 ...

  6. 下面介绍mysql中模糊查询的四种用法:

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

  7. Java JDBC 模糊查询 避免输入_,%返回全部数据

    Java JDBC 模糊查询 避免输入_,%返回全部数据 "SELECT * FROM employees WHERE INSTR(first_name,?)>0 " 仅供参 ...

  8. 表单模糊查询的三种简单方式(springboot-h2-mybatis)

    前几天运营提到说后台管理系统有几个地方想要模糊查询..   想了下是简单的,就是要注意以前方法的被调用情况,进行增量改动,以免牵一发而动全身.整理一波记录下(本次案例是按名字模糊查询学生信息). 三种 ...

  9. MyBatis 模糊查询的 4 种实现方式

    引言 MyBatis 有 4 种方式可以实现模糊查询. 员工信息表 ( tb_employee ) 如下: id name sex email birthday address 001 张一凡 男 z ...

随机推荐

  1. 工具系列 | 使用FormData方式上传文件

    服务端代码 /** * 文件上传 */ public function uploadFile() { Log::error('文件上传 : '.json_encode($_FILES)); $dir ...

  2. 解决ffmpeg拉流转发频繁丢包问题max delay reached. need to consume packet

    软件: 1.流媒体服务器EasyDarwin-windows-8.1.0-1901141151 2.ffmpeg-20181001-dcbd89e-win64-static 3.直播源:rtsp:// ...

  3. Embed MP4 in HTML using flash-player(html5 video player)

    https://stackoverflow.com/questions/1000851/embed-mp4-in-html-using-flash-player ******************* ...

  4. Python3基础 None 使用is来判断

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  5. 解决IDEA打Jar包找不到或无法加载主类问题

    转自以下博主内容:https://blog.csdn.net/qq_43189115/article/details/99856659 就是因为打包一直提示有问题,网上很多帖子试过没有用,就是这个博主 ...

  6. shell基础知识6-在不按回车键的情况下读入N个字符

    Bash命令 read 能够从键盘或标准输入中读取文本.我们可以使用 read 以交互的形式读取用 户输入,不过 read 能做的可远不止这些.编程语言的大多数输入库都是从键盘读取输入,当回 车键按下 ...

  7. Clojure 哲学

    简单性.专心编程不受打扰(freedom to focus).给力(empowerment).一致性和明确性:Closure编程语言中几乎每一个元素的设计思想都是为了促成这些目标的实现. 学习一门新的 ...

  8. axios get,post请求时带headers

    axios post请求时带headers: axios.post("http://xxx.com/xxx/xxx/xxx?", { 'queslistid': this.kemu ...

  9. ABP .NETCore更新数据库时一直连接的之前数据库

    使用Update-Database -Verbose更新数据库时,在appsettings.json配置文件中已修改为新的连接字符串,但是使用命令更新数据库时仍然连接的是之前的数据库. 后来把代码移至 ...

  10. npx vs npm

    npx vs npm npx 使用教程