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;
  }

 

慕粉3952420
浏览 6254回答 1
1回答

大咪

因为你sql语句写的是限定商品名称查询呀,where后面你写的是什么就是限定是什么,比如你这个写的是商品名称,就是对商品名称的模糊查询!
打开App,查看更多内容
随时随地看视频慕课网APP