为什么我删除分类列表时,也会把里边商品删除?
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;
}
}
}
}
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;
}
}
}
}
点击删除报错:
java.sql.SQLIntegrityConstraintViolationException: Column 'cid' cannot be null
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:955)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1094)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1042)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1345)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1027)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at com.imooc.dao.impl.ProductDaoImpl.update(ProductDaoImpl.java:220)
at com.imooc.service.impl.CategoryServiceImpl.delete(CategoryServiceImpl.java:67)
at com.imooc.web.action.CategoryServlet.delete(CategoryServlet.java:165)
at com.imooc.web.action.CategoryServlet.doGet(CategoryServlet.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.imooc.web.filter.PrivilegeFilter.doFilter(PrivilegeFilter.java:34)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.imooc.web.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:25)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:844)
正在回答
同学你好,1、删除分类列表时,将该分类下的所有商品进行了处理,取消了关联
2、删除分类报错,报出Column 'cid' cannot be null-->列cid不能为空
综上所示可知,在删除分类时,要求cid不能为null,同学可以排查一下自己数据库中product里cid是不是要求了不能为null,比如:
如上所示,检查自己的cid字段,取消不能为null的限制。再来试试。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星