为了账号安全,请及时绑定邮箱和手机立即绑定

向mysql数据库中插入数据时报错

向mysql数据库中插入数据时报错

蝴蝶刀刀 2019-03-21 22:19:57
public class categorydao {     public static void save(category c) throws SQLException{         Connection conn=(Connection) DB.getConnection();         String sql=null;         if(c.getId()==-1){             sql="insert into category values(null,?,?,?,?,?)";           }else{           sql="insert into category values("+c.getId()+",?,?,?,?,?)";           }          PreparedStatement ps=conn.prepareStatement(sql);         try {             ps.setString(1,c.getName() );             ps.setString(2,c.getDescr());             ps.setInt(3, c.getPid());             ps.setInt(4, c.isIsleaf()?0:1);             ps.setInt(5, c.getGrade());             ps.executeUpdate();         } catch (Exception e) {             // TODO Auto-generated catch block             e.printStackTrace();         }finally{             DB.free(null, ps, conn);         }     }错误如下:(反正插入不了)com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
查看完整描述

3 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

字段全包含了?是一一对应的吗


查看完整回答
反对 回复 2019-04-28
  • 3 回答
  • 0 关注
  • 720 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信