Ola galera.
Estou com um problema no insert_last_id, tenho duas tabelas onde a chave primaria de uma delas é chave estrangeira na outra.
quando eu ensiro os primeiros dados, insere de boa
quando vou fazer uma nova inserção da o seguinte erro:
Exception in thread "main" java.lang.RuntimeException: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 1
at Persistencia.inserirDados(Persistencia.java:34)
at Principal.main(Principal.java:107)
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:794)
at Persistencia.inserirDados(Persistencia.java:31)
... 1 more
como posso resolver isso?
acho que tem q ter um contador para o id, como posso fazer?
veja um trecho do código:
public void inserirDados(Dados d) {
this.sql = "insert into registro (reg_num,reg_hora,reg_data,reg_can1,reg_can2,reg_pla_id) values (?,?,?,?,?,LAST_INSERT_ID())";
try {
PreparedStatement stmt = this.connection.prepareStatement(this.sql);
// seta os valores
stmt.setString(1, d.getNum());
stmt.setString(2, d.getHora());
stmt.setString(3, d.getData());
stmt.setString(4, d.getCanal1());
stmt.setString(5, d.getCanal2());
// executa
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void planilha(Planilha p) {
this.sql = "insert into planilha (pla_end,pla_cid,pla_reg,pla_ano) values (?,?,?,?)";
try {
PreparedStatement stmt = this.connection.prepareStatement(this.sql);
// seta os valores
stmt.setString(1, p.getEndereco());
stmt.setString(2, p.getCidade());
stmt.setString(3, p.getRegional());
stmt.setString(4, p.getAno());
// executa
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}