怎么解决MySQLIntegrityConstraintVi…java异常

作者:暴躁小n | 创建时间: 2023-07-17
怎么解决java中com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException呢?我统计了会产生MySQLIntegrityConstraintViol...
怎么解决MySQLIntegrityConstraintVi…java异常

操作方法

下图是MySQLIntegrityConstraintViolationException异常的源码,图中的①②③④是MySQLIntegrityConstraintViolationException对象的4种构造器。参数的含义分别是:reason(原因), String SQLState(SQL状态), int vendorCode(数据库中的错误码)。

下图是经常会遇到的MySQLIntegrityConstraintViolationException异常。

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row 引起这个的原因主要是为数据表插入或更新数据时,此表中的某个字段做了其他表的外键PK。

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row:引起这个的原因主要是因为删除或更新数据表中的数据时,此表中的某个字段做了其他表的外键PK。

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '******' for key 'PRIMARY',引起这个的原因是因为插入的数据,主键的值重复。

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column cannot be null,引起这个的原因是表示,主键的列不能为null值。

点击展开全文

更多推荐