Publicclasscategorydao {publicstaticvoidsave (categoryc) throwsSQLException {Connectionconn (Connection) DB. getConnection (); Stringsqlnull; if (c. getId ()-1) {SQL & quot; insertintocategoryvalues (null ,?,?,?,?,?) & Quot;} else {SQL & quot; in mysqljavaweb
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); } }
The error is as follows: (cannot be inserted anyway)
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'