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