gpt4 book ai didi

Java : JDBC Delete Row Issue

转载 作者:行者123 更新时间:2023-11-29 21:57:55 26 4
gpt4 key购买 nike

这是GUI程序,当单击按钮时它会删除该行。但问题是它给出这样的异常

Cannot delete or update a parent row: a foreign key constraint fails (`sakila`.`film_actor`, CONSTRAINT `fk_film_actor_actor` FOREIGN KEY (`actor_id`) REFERENCES `actor` (`actor_id`) ON UPDATE CASCADE)

我不知道这是什么意思,但我想删除该行,有什么方法可以解决这个问题。这是架构和表的快照。 enter image description here
代码:

public class GUI extends JFrame {

public GUI() {
super("Frame");
setLayout(new FlowLayout());

JButton b1 = new JButton("Click Delete Row");
b1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

try {

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/cinema", "hussnain", "toot");

String query = "delete from sakila.actor where actor_id=3";
PreparedStatement pre = conn.prepareStatement(query);

pre.executeUpdate();
JOptionPane.showMessageDialog(null, "Query Executed");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1.getMessage());

e1.printStackTrace();
}

}

});

add(b1);

setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);

}

}

主要方法

public class Main {

public static void main(String[] args) {

GUI obj = new GUI();


}

}

最佳答案

您对 film_actor 表有外键约束,因此在从 actor 表中删除之前,您应该首先从 film_actor 表中删除。

您也可以禁用外键检查,但这会导致数据不一致;

SET FOREIGN_KEY_CHECKS=0; -- disable
SET FOREIGN_KEY_CHECKS=1; -- re-enable

关于Java : JDBC Delete Row Issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32951273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com