老师 保存商品失败
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");
}
比如我添加一个
结果却没有
正在回答
同学你好,建议同学在mysql中执行一下如下的sql语句,查看显示几条数据:
1 | select * from product p,category c where p.cid=c.cid order by p.pid DESC; |
另外return list;应该写在最后,如:
这样是遍历完所有的结果集才返回list集合。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
保存是成功了 但是为什么数据只能展示一条记录
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>
是代码问题吗
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧