public Goods findGoodsById(String gid) { Goods goods = null; try { goods = (List)this.qRunner.query("select * from goods where gid = ?", new BeanHandler(Goods.class), new Object[]{gid}); } catch (SQLException var4) { var4.printStackTrace(); } return goods; }
时间: 2024-02-14 08:13:44 浏览: 138
这段代码有一个错误,将查询结果的类型声明为了List,而实际上使用的是BeanHandler,应该将查询结果的类型声明为Goods,否则编译时会出现类型不匹配的错误。正确的代码应该是这样的:
```
public Goods findGoodsById(String gid) {
Goods goods = null;
try {
goods = this.qRunner.query("select * from goods where gid = ?", new BeanHandler<>(Goods.class), gid);
} catch (SQLException e) {
e.printStackTrace();
}
return goods;
}
```
这个方法的作用是根据商品ID查询商品信息,并将查询结果映射为一个Goods对象。它使用了Apache Commons DbUtils库中的QueryRunner类来执行SQL查询语句,并通过BeanHandler类将查询结果映射为一个JavaBean对象(即Goods类的实例)。具体来说,这个方法接受一个字符串参数gid,表示商品ID,返回一个Goods对象,表示查询到的商品信息。如果查询失败,则返回null。
相关问题
public Goods findGoodsById(String gid) { Goods goods = null; try { goods = (Goods)this.qRunner.query("select * from goods where gid = ?", new BeanHandler(Goods.class), new Object[]{gid}); } catch (SQLException var4) { var4.printStackTrace(); } return goods; }
这段代码是一个Java方法,用于根据商品ID查询商品信息。它使用了Apache Commons DbUtils库中的QueryRunner类来执行SQL查询语句,并通过BeanHandler类将查询结果映射为一个JavaBean对象(即Goods类的实例)。具体来说,这个方法接受一个字符串参数gid,表示商品ID,返回一个Goods对象,表示查询到的商品信息。如果查询失败,则返回null。
public int deleteGoods(String ids) { int row = 0; try { row = this.qRunner.update("delete from goods where gid in (" + ids + ")"); } catch (SQLException var4) { var4.printStackTrace(); } return row; }
这段代码的作用是根据传入的商品id列表ids批量删除商品信息,并返回删除的行数。其中,QRunner是一个轻量级的JDBC操作库。在方法中,通过拼接SQL语句将需要删除的商品id列表传给 QRunner 的 update 方法,实现了批量删除商品信息的功能。如果删除过程中出现异常,会捕获 SQLException 异常并打印出错信息。最后,返回删除的行数。
阅读全文
相关推荐














<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="com.test.bean.Goods,java.util.ArrayList" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>购物车</title> <style type="text/css"> table{border-collapse:collapse;} td{border:1px solid black; text-align:center; } #deal{margin-left:200px} </style> </head> <body> <jsp:useBean id="cart" class="com.test.bean.Cart" scope="session"></jsp:useBean> <%if(cart==null||cart.getGoodslist().size()==0) out.println("购物车空空如也.....返回商品首页"); else{ ArrayList<Goods>goodslist=cart.getGoodslist(); %> 当前购物车共有<%=cart.getGcount() %>件物品 返回商品首页
序号 商品号 商品名称 价格 数量 小计 描述 <%for(int i=0;i<goodslist.size();i++){ Goods goods=goodslist.get(i); %> <%=i+1 %> <%=goods.getGid() %> <%=goods.getGname() %> <%= String.format("%.2f", goods.getGprice())%> <%=goods.getGcount() %> <%=String.format("%.2f", goods.getGprice()*goods.getGcount()) %> 删除 <%}%>
总计<%=String.format("%.2f",cart.getTotal())%> <input type="button" id="deal" value="购买" /></input> <%}%> <script type="text/javascript"> var dealbutton=document.getElementById("deal"); dealbutton.onclick=function(){ var cf=confirm("确定购买吗?"); if(cf==true){ window.location.href="Servlet/DealServlet"; } } </script> </body> </html> 修改删除功能,在购物车物品数量大于1的情况下点击“删除”时减少1个,等于1时删除此项商品;

