老师 保存商品失败

老师 保存商品失败

  public void save(Product product) {
       Connection conn = null;
       PreparedStatement pstmt = null;
       try {
           conn = JDBCUtils.getConnection();
           String sql = "insert into product values(null,?,?,?,?,?,?,?)";
           pstmt = conn.prepareStatement(sql);
           pstmt.setString(1, product.getPname());
           pstmt.setString(2, product.getAuthor());
           pstmt.setDouble(3, product.getPrice());
           pstmt.setString(4, product.getDescription());
           pstmt.setString(5, product.getFilename());
           pstmt.setString(6, product.getPath());
           pstmt.setInt(7, product.getCategory().getCid());
           pstmt.executeUpdate();
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           JDBCUtils.release(pstmt, conn);
       }
   }
}

private void save(HttpServletRequest request, HttpServletResponse response) throws IOException ,ServletException {
   //文件的上传
  Map<String,String> map=UploadUtils.uploadFile(request);
   //封装数据
   Product product=new Product();
   product.setPname(map.get("pname"));
   product.setAuthor(map.get("author"));
   product.setPrice(Double.parseDouble(map.get("price")));
   product.setDescription(map.get("description"));
   product.setFilename(map.get("filename"));
   product.setPath(map.get("path"));
   product.getCategory().setCid(Integer.parseInt(map.get("cid")));
   //调用数据
   ProductService productService=new ProductServiceImpl();
   productService.save(product);
   //页面跳转
   response.sendRedirect(request.getContextPath()+"/ProductServlet?method=findAll");
}

比如我添加一个http://img1.sycdn.imooc.com//climg/5d24a28a0001109610700693.jpg

http://img1.sycdn.imooc.com//climg/5d24a30300019e2714120276.jpg结果却没有

正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学你好,建议同学在mysql中执行一下如下的sql语句,查看显示几条数据:

1
select * from product p,category c where p.cid=c.cid  order by p.pid DESC;

另外return list;应该写在最后,如:

http://img1.sycdn.imooc.com//climg/5d2585920001abe107010535.jpg

这样是遍历完所有的结果集才返回list集合。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

提问者 学习中的傅山 2019-07-10 11:35:32

http://img1.sycdn.imooc.com//climg/5d255c330001aa9607350067.jpg

http://img1.sycdn.imooc.com//climg/5d255c430001fa1a13870205.jpg

保存是成功了  但是为什么数据只能展示一条记录

public List<Product> findAll() {
   Connection conn = null;
   PreparedStatement pstmt = null;
   ResultSet rs = null;
   List<Product> list = null;
   try {
       //获得连接
       conn = JDBCUtils.getConnection();
       //编写sql
       String sql = "select * from product p,category c where p.cid=c.cid  order by p.pid DESC ";
       //预编译sql
       pstmt = conn.prepareStatement(sql);
       //执行sql
       rs = pstmt.executeQuery();
       //遍历结果集
       list = new ArrayList<Product>();
       while (rs.next()) {
           Product product = new Product();
           product.setPid(rs.getInt("pid"));
           product.setPname(rs.getString("pname"));
           product.setAuthor(rs.getString("author"));
           product.setPrice(rs.getDouble("price"));
           product.setDescription(rs.getString("description"));
           product.setFilename(rs.getString("filename"));
           product.setPath(rs.getString("path"));
           //封装商品所属的分类
           product.getCategory().setCid(rs.getInt("cid"));
           product.getCategory().setCname(rs.getString("cname"));
           product.getCategory().setCdesc(rs.getString("cdesc"));
           list.add(product);
           return list;
       }

   } catch (Exception e) {
       e.printStackTrace();
   } finally {
       JDBCUtils.release(rs, pstmt, conn);
   }
  <div class="panel-body pn">
            <table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
                <thead>
                <tr class="">
                    <th class="hidden-xs">名称</th>
                    <th class="hidden-xs">分类</th>
                    <th class="hidden-xs">价格</th>
<th class="hidden-xs">作者</th>
<th class="hidden-xs">描述</th>
<th>操作</th>
                </tr>
                </thead>
                <tbody>
                  <c:forEach var="product" items="${ list }">
                    <tr class="message-unread">
                        <td>${ product.pname }</td>
                        <td>${ product.category.cname }</td>
                        <td>${ product.price }</td>
                        <td>${ product.author }</td>
  <td>${ product.description }</td>
                        <td>
                            <a href="${ pageContext.request.contextPath }/ProductServlet?method=edit&pid=${ product.pid }">编辑</a>
                            <a href="${ pageContext.request.contextPath }/ProductServlet?method=delete&pid=${ product.pid }">删除</a>
                        </td>
                    </tr>
</c:forEach>

是代码问题吗

好帮手慕阿满 2019-07-10 11:04:03

同学你好,建议同学去数据库中查询一下是否存在添加的商品。另外同学可以在如下添加int n = pastmt.executeUpdate()并输出n,查看执行sql影响的行数是否大于,也就是是否添加了数据。

http://img1.sycdn.imooc.com//climg/5d25556f000146a604180105.jpg

祝:学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码