要实现模糊查询,首先得明白所使用的数据库到底是什么,对应的语句又是什么;
一般而言模糊查询要根据所搜索的内容进行模糊匹配;
关键字like的实现
例如:select * from table where name like %yugi%;

1:java报表开发工具中如何进行模糊查询
模糊查询是利用“_”表示单个字符和“%”表示任意个字符进行匹配的。一些常见的格式如下:

Select * from 表名 where 列名 like '%'; //查询出全部

Select * from 表名 where 列名 like 'x'; //完全匹配查询

Select * from 表名 where 列名 like '_x'; //右为x,前面有一位字符

Select * from 表名 where 列名 like '__x'; //右为x,前面有两位位字符

Select * from 表名 where 列名 like 'x___'; //左为x,后面有两位位字符

Select * from 表名 where 列名 like '%x'; //右为x,前面可以有任意位字符

Select * from 表名 where 列名 like 'x%'; //左为x,后面可以有任意位字符

Select * from 表名 where 列名 like '%x%'; //中间为x,左右都可以有任意位字符

结合参数的模糊查询(用${name}代表上述的x):

Select * from 表名 where 列名 like '${name}';
Select * from 表名 where 列名 like '%${name}'

以此类推。

2:单条件模糊查询
单条件模糊查询,就是根据一个字段查询,模糊匹配%变量%

public class SalDaoImpl extends DbConn{public List<Sal> findByMap(String empno) {
  List<Sal> list =new ArrayList<Sal>();
  if(conn!=null) {
   try{
    String sql="select * from [dbo].[sal] where empno  like '%"+empno+"'";
    Statement stmt = conn.createStatement();     
    ResultSet rs = stmt.executeQuery(sql);     
    while(rs.next()){
     Sal r =new Sal();
     r.setId(rs.getInt("id"));
     r.setEmpno(rs.getString("empno"));
     r.setName(rs.getString("name"));
     r.setCreateDate(rs.getString("createDate"));
     r.setSal(rs.getDouble("sal"));
     r.setSalDecrease(rs.getDouble("salDecrease"));
     list.add(r);
    }
    rs.close(); 
   }catch(Exception e){
    e.printStackTrace()}
  }
  return list;
  }

public static void main(String args[]){

List<Sal> list=new SaoImpl().findByMap("10005");

}

}

2:多条件模糊查询

根据多个参数来查询,也就是多条件查询

public class SalDaoImpl extends DbConn{public List<Sal> findByMap(String empno,String createDate) {
  List<Sal> list =new ArrayList<Sal>();
  if(conn!=null) {
   try{
    String sql="select * from [dbo].[sal] where empno  like '%"+empno+"'and createDate like'%"+createDate+"'";
    Statement stmt = conn.createStatement();     
    ResultSet rs = stmt.executeQuery(sql);     
    while(rs.next()){
     Sal r =new Sal();
     r.setId(rs.getInt("id"));
     r.setEmpno(rs.getString("empno"));
     r.setName(rs.getString("name"));
     r.setCreateDate(rs.getString("createDate"));
     r.setSal(rs.getDouble("sal"));
     r.setSalDecrease(rs.getDouble("salDecrease"));
     list.add(r);
    }
    rs.close(); 
   }catch(Exception e){
    e.printStackTrace();
   }
  }
  return list;
 }
 }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐