如何从JDBC截断Postgresql的表

Oli*_*ire 3 java postgresql jdbc

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

这是我尝试过的,但没有一个工作......甚至没有报告任何错误:

CallableStatement.

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

Statement.

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

PreparedStatement.

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

Oli*_*ire 11

截断后,我需要提交:

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

文档:

TRUNCATE对于表中的数据是事务安全的:如果周围的事务没有提交,则截断将安全地回滚.