• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

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

像下面这个只能搜到一条数据,怎么把所有包含关键字的数据都搜出来
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

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

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!