为了账号安全,请及时绑定邮箱和手机立即绑定

sql语句like模糊搜索怎么通过一个关键字搜到所有包含关键字的数据

sql语句like模糊搜索怎么通过一个关键字搜到所有包含关键字的数据

慕粉3952420 2016-09-07 14:00:27
像下面这个只能搜到一条数据,怎么把所有包含关键字的数据都搜出来 String strSql = "select 商品ID,商品名称,价格,优惠价格,图片地址  from 商品数据表  where 商品名称  like ?";      GoodsInfoManage5_8 gm = new GoodsInfoManage5_8();      try {        goods = gm.getInfoName(strSql, findname);        request.setAttribute("goods", goods);        request.getRequestDispatcher("productList.jsp").forward(request, response);              }      catch (SQLException e) {        e.printStackTrace();      } GoodsInfoManage5_8():String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ECommerce";  String username = "sa";  String password = "123456";  private Connection conn = null;  private Statement stmt = null;  private ResultSet rs = null;  private PreparedStatement pstmt = null;public GoodsInfo getInfoName(String sql, String name) throws SQLException {    ResultSet rs = null;    GoodsInfo goods = null;    if (this.conn == null)      Connection();    try    {      this.pstmt = this.conn.prepareStatement(sql);      this.pstmt.setString(1, "%" + name + "%");      rs = this.pstmt.executeQuery();      if(rs.next()){        goods = new GoodsInfo();        goods.setGoodsId(rs.getInt("商品ID"));        goods.setGoodsName(rs.getString("商品名称"));        goods.setGoodsPrice(rs.getDouble("价格"));        goods.setGoodsPreferentialPrice(rs.getDouble("优惠价格"));        goods.setGoodsImageAddress(rs.getString("图片地址"));      }    }    catch (SQLException e) {      e.printStackTrace();    }    return goods;  } 
查看完整描述

1 回答

?
大咪

TA贡献785条经验 获得超332个赞

因为你sql语句写的是限定商品名称查询呀,where后面你写的是什么就是限定是什么,比如你这个写的是商品名称,就是对商品名称的模糊查询!

查看完整回答
反对 回复 2016-09-07
  • 慕粉3952420
    慕粉3952420
    确实如此,但是我要做到的功能是查询到多条包含此名称的数据,这个只能查询到一条
  • 大咪
    大咪
    不应该呀,你这个模糊查询应该就是包含所有关键字段了。。。因为是在name两边加的%%所以应该按理来说就是只要符合你输入的字段就应该都能显示啊。。除非是你JSP上边的那个没有修改。。。你是用JSP做的吗?
  • 1 回答
  • 0 关注
  • 6250 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信