gpt4 book ai didi

java - 如何从 JDBC 截断 Postgresql 的表

转载 作者:搜寻专家 更新时间:2023-11-01 04:07:57 25 4
gpt4 key购买 nike

我有一个 Postgresql 数据库,我想使用 JDBC 截断一些表。我该怎么做?

这是我尝试过的方法,但没有任何效果......甚至没有报告任何错误:

使用 CallableStatement

try (Connection connection = getConnection();
CallableStatement statement = connection.prepareCall("TRUNCATE " + tableName)) {
return statement.execute();
}

使用语句

try (Connection connection = getConnection();
Statement statement = connection.createStatement()) {
return statement.execute("TRUNCATE " + tableName);
}

使用PreparedStatement

try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement("TRUNCATE " + tableName)) {
return statement.execute();
}

最佳答案

截断后,我需要提交:

try (Connection connection = getConnection();
Statement statement = connection.createStatement()) {
int result = statement.executeUpdate("TRUNCATE " + tableName);
connection.commit();
return result;
}

来自 the documentation :

TRUNCATE is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not commit.

关于java - 如何从 JDBC 截断 Postgresql 的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22018225/

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