为什么控制台输出null?我的数据库里边是有数据的

package com.imooc.web.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.domain.Category;
import com.imooc.domain.PageBean;
import com.imooc.domain.Product;
import com.imooc.service.CategoryService;
import com.imooc.service.ProductService;
import com.imooc.service.impl.CategoryServiceImpl;
import com.imooc.service.impl.ProductServiceImpl;
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int page=0;
String currPage=request.getParameter("page");
if(currPage == null) {
page=1;
}else {
page=Integer.parseInt(currPage);
}
//查询操作
//查询所有分类
CategoryService categoryService=new CategoryServiceImpl();
List<Category> categoryList = categoryService.findAll();
//分页查询商品数据
ProductService productService=new ProductServiceImpl();
PageBean<Product> pageBean = productService.findByPage(page);
System.out.println(pageBean);
//页面跳转
request.setAttribute("categoryList", categoryList);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.imooc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.imooc.dao.ProductDao;
import com.imooc.domain.Product;
import com.imooc.utils.JDBCUtils;
/**
* 商品实现类实现接口
* @author 14628
*
*/
public class ProductDaoImpl implements ProductDao{
@Override
public List<Product> findAll() {
System.out.println("ProductDao的findAll方法执行了");
Connection conn=null;
PreparedStatement psta=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
psta=conn.prepareStatement(sql);
//执行SQL
rs=psta.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);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(rs, psta, conn);
}
return list;
}
@Override
public void save(Product product) {
Connection conn=null;
PreparedStatement psta=null;
try {
//获得连接
conn=JDBCUtils.getConnection();
String sql="INSERT INTO product VALUES(null,?,?,?,?,?,?,?)";
psta=conn.prepareStatement(sql);
psta.setString(1, product.getPname());
psta.setString(2, product.getAuthor());
psta.setDouble(3, product.getPrice());
psta.setString(4, product.getDescription());
psta.setString(5, product.getFilename());
psta.setString(6, product.getPath());
psta.setInt(7, product.getCategory().getCid());
psta.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(psta, conn);
}
}
@Override
public Product findOne(Integer pid) {
Connection conn=null;
PreparedStatement psta=null;
ResultSet rs=null;
try {
conn=JDBCUtils.getConnection();
String sql="SELECT * FROM product p,category c WHERE p.cid=c.cid AND p.pid=?";
psta=conn.prepareStatement(sql);
psta.setInt(1, pid);
rs=psta.executeQuery();
if(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"));
return product;
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(rs,psta, conn);
}
return null;
}
@Override
public void update(Product product) {
Connection conn=null;
PreparedStatement psta=null;
try {
//获得连接
conn=JDBCUtils.getConnection();
String sql="UPDATE product SET pname=?,author=?,price=?,description=?,filename=?,path=?,cid=? WHERE pid=?";
psta=conn.prepareStatement(sql);
psta.setString(1, product.getPname());
psta.setString(2, product.getAuthor());
psta.setDouble(3, product.getPrice());
psta.setString(4, product.getDescription());
psta.setString(5, product.getFilename());
psta.setString(6, product.getPath());
psta.setObject(7,product.getCategory().getCid());
psta.setInt(8, product.getPid());
psta.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(psta, conn);
}
}
@Override
public void delete(Integer pid) {
Connection conn=null;
PreparedStatement psta=null;
try {
conn=JDBCUtils.getConnection();
String sql="DELETE FROM product WHERE pid=?";
psta=conn.prepareStatement(sql);
psta.setInt(1,pid);
psta.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(psta, conn);
}
}
@Override
public List<Product> finByCid(Integer cid) {
Connection conn=null;
PreparedStatement psta=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 and p.cid = ? ORDER BY p.pid DESC";
//预编译SQL
psta=conn.prepareStatement(sql);
//设置参数
psta.setInt(1, cid);
//执行SQL
rs=psta.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);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(rs, psta, conn);
}
return list;
}
//重写update方法,在业务层中获得连接
@Override
public void update(Connection conn, Product product) {
PreparedStatement psta=null;
try {
String sql="UPDATE product SET pname=?,author=?,price=?,description=?,filename=?,path=?,cid=? WHERE pid=?";
psta=conn.prepareStatement(sql);
psta.setString(1, product.getPname());
psta.setString(2, product.getAuthor());
psta.setDouble(3, product.getPrice());
psta.setString(4, product.getDescription());
psta.setString(5, product.getFilename());
psta.setString(6, product.getPath());
psta.setObject(7,product.getCategory().getCid());
psta.setInt(8, product.getPid());
psta.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
if(psta != null) {
try {
psta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
psta =null;
}
}
}
//查询商品的记录数
@Override
public int finCount() {
Connection conn=null;
PreparedStatement psta=null;
ResultSet rs=null;
Long count=0L;
try {
conn=JDBCUtils.getConnection();
String sql="SELECT count(*) AS count FROM product";
psta=conn.prepareStatement(sql);
rs=psta.executeQuery();
if(rs.next()) {
count = rs.getLong("count");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(rs, psta, conn);
}
return count.intValue();
}
@Override
public List<Product> findByPage(int begin, int limit) {
Connection conn=null;
PreparedStatement psta=null;
ResultSet rs=null;
List<Product> list =null;
try {
conn=JDBCUtils.getConnection();
String sql="SELECT * FROM product limit ?,?";
psta=conn.prepareStatement(sql);
psta.setInt(1, begin);
psta.setInt(2, limit);
rs=psta.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"));
list.add(product);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(rs, psta, conn);
}
return list;
}
}
package com.imooc.service.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import com.imooc.dao.CategoryDao;
import com.imooc.dao.ProductDao;
import com.imooc.dao.impl.CategoryDaoImpl;
import com.imooc.dao.impl.ProductDaoImpl;
import com.imooc.domain.Category;
import com.imooc.domain.Product;
/**
* 分类的Serive实现接口
*/
import com.imooc.service.CategoryService;
import com.imooc.service.ProductService;
import com.imooc.utils.JDBCUtils;
public class CategoryServiceImpl implements CategoryService {
@Override
public List<Category> findAll() {
System.out.println("CategoryService的findAll方法执行了");
CategoryDao categoryDao=new CategoryDaoImpl();
return categoryDao.findAll();
}
@Override
public void save(Category category) {
CategoryDao CategoryDao=new CategoryDaoImpl();
CategoryDao.save(category);
}
@Override
public Category findOne(Integer cid) {
CategoryDao categoryDao = new CategoryDaoImpl();
return categoryDao.findOne(cid);
}
@Override
public void update(Category category) {
CategoryDao categoryDao=new CategoryDaoImpl();
categoryDao.update(category);
}
@Override
public void delete(Integer cid) {
/**
* 事务管理:在业务层统一创建连接对象,保证多个DAO中使用同一个连接
* 1.创建连接以后,将连接对象传给DAO
* 2.创建一个连接对象,将连接绑定到线程中(ThreadLocal)
* */
Connection conn=null;
try {
// 获得连接
conn=JDBCUtils.getConnection();
//开启事务:把自动提交关闭
conn.setAutoCommit(false);
// 要在删除分类之前,先将所属该分类的商品处理一下
ProductDao productDao = new ProductDaoImpl();
List<Product> list = productDao.finByCid(cid);
// 将查询出分类的商品设为NULL
for (Product product : list) {
product.getCategory().setCid(null);
productDao.update(conn,product);
}
// 删除分类
CategoryDao categoryDao = new CategoryDaoImpl();
categoryDao.delete(conn,cid);
//提交事务:手动提交
conn.commit();
} catch (Exception e) {
//回滚事务:
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn =null;
}
}
}
}

正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星