Home > Database > Mysql Tutorial > java-向mysql数据库中插入数据时报错

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

WBOY
Release: 2016-06-06 09:45:51
Original
1392 people have browsed it

mysqljavaweb

<code>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);        }    }</code>
Copy after login

错误如下:(反正插入不了)

<code>com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template