Boa Tarde, Galera
Gente estou tentando apagar um registro de uma tabela, porem essa tb esta relacionada com outra.
Quero excluir um medicamento e todos os lotes relacionados a ele.
Estou usando essa instrução:
public void excluirMedicamento (int id) throws Exception{
getConexao();
String query = "DELETE FROM medicamento where idproduto=?";
pstmt = con.prepareStatement(query);
pstmt.setInt(1, id);
pstmt.execute();
}
public void excluirLote (int id) throws Exception {
getConexao();
String query = "DELETE FROM lote where medicamento_idproduto=?";
pstmt = con.prepareStatement(query);
pstmt.setInt(1, id);
pstmt.execute();
}
public boolean excluirMedicamento() {
boolean retorno = false;
try {
EstoqueDAO pd = new EstoqueDAO();
if (CampObrig(txtidmedicamento)) {
JOptionPane.showMessageDialog(null,
"Todos os campos são obrigatórios!!", "Atenção",
JOptionPane.PLAIN_MESSAGE);
} else {
if (pd.constlote(Integer.parseInt(txtidmedicamento.getText()))) {
/*int i = JOptionPane
.showConfirmDialog(
null,
"Existem lotes cadastrados para esse medicamento, caso confirme a exclusão esses \n"
+ "lotes também serão excluidos!!! \n\nCaso não deseje excluir os lotes desse medicamento, \n"
+ "antes de exclui-ló mova o lote para um novo medicamento, \n"
+ "usando para isso a opção: Mover Lote.",
"Atenção!!!", JOptionPane.YES_NO_OPTION);*/
if (JOptionPane
.showConfirmDialog(
null,
"Existem lotes cadastrados para esse medicamento, caso confirme a exclusão esses \n"
+ "lotes também serão excluidos!!! \n\nCaso não deseje excluir os lotes desse medicamento, \n"
+ "antes de exclui-ló mova o lote para um novo medicamento, \n"
+ "usando para isso a opção: Mover Lote.",
"Atenção!!!", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
pd.excluirMedicamento(Integer.parseInt(txtidmedicamento
.getText()));
pd.excluirLote(Integer.parseInt(txtidmedicamento
.getText()));
retorno = true;
} else {
JOptionPane.showMessageDialog(null,
"Operação cancelada!!!");
}
} else {
pd.excluirMedicamento(Integer.parseInt(txtidmedicamento
.getText()));
retorno = true;
}
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane
.showMessageDialog(
null,
"Erro Geral, por favor entre em contato com o Administrador do Sistema.",
"Atenção", JOptionPane.ERROR_MESSAGE);
}
return retorno;
}
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`siscef_db`.`lote`, CONSTRAINT `fk_Lote_Medicamento1` FOREIGN KEY (`medicamento_idproduto`) REFERENCES `medicamento` (`idproduto`) ON DELETE NO ACTION ON UPDATE NO ACTION)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362)
at persistencia.EstoqueDAO.excluirMedicamento(EstoqueDAO.java:153)
at visao.JanelaExcluir.excluirMedicamento(JanelaExcluir.java:134)
at visao.JanelaExcluir$4.actionPerformed(JanelaExcluir.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)