使用单个命中从多个表中删除数据库

时间:2019-06-24 10:35:38

标签: java mysql hibernate spring-boot jpa

我有7个表T1,T2,T3,T4,T5,T6和T7。所有这些表都有一个相同的列c1。假设c1中的值等于“ abc100”。如何一次单击数据库(使用JPA)对所有表执行删除操作?

1 个答案:

答案 0 :(得分:0)

很抱歉,您无法一次执行多个删除查询。

但是您可以使用下面发布的代码,这些代码可以帮助您了解有关删除多个表中数据的更多信息。

try (Connection connection = **strong text**DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password")) {

   try (PreparedStatement stmt = connection.prepareStatement("DELETE * FROM PUBLISHER1 WHERE ID=1")) {
       stmt.executeUpdate();
   }
   // stmt is auto closed here, even if SQLException is thrown

   try (PreparedStatement stmt = connection.prepareStatement("DELETE *FROM PUBLISHER2 WHERE ID=2");
       stmt.executeUpdate();
   }
   // stmt is auto closed here, even if SQLException is thrown
}
// connection is auto closed here, even if SQLException is thrown