Java如何解决模糊查询(数据库SQL语句,报表开发工具) |
您所在的位置:网站首页 › java代码查询 › Java如何解决模糊查询(数据库SQL语句,报表开发工具) |
要实现模糊查询,首先得明白所使用的数据库到底是什么,对应的语句又是什么; 一般而言模糊查询要根据所搜索的内容进行模糊匹配; 关键字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 findByMap(String empno) { List list =new ArrayList(); 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 list=new SaoImpl().findByMap("10005"); } }2:多条件模糊查询 根据多个参数来查询,也就是多条件查询 public class SalDaoImpl extends DbConn{public List findByMap(String empno,String createDate) { List list =new ArrayList(); 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; } } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |