我有一个Postgresql数据库,我想使用JDBC截断一些表。我该怎么做?
这是我尝试过的,但没有一个工作......甚至没有报告任何错误:
使用CallableStatement
。
try (Connection connection = getConnection();
CallableStatement statement = connection.prepareCall("TRUNCATE " + tableName)) {
return statement.execute();
}
使用Statement
。
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();
}
答案 0 :(得分:10)
截断后,我需要提交:
try (Connection connection = getConnection();
Statement statement = connection.createStatement()) {
int result = statement.executeUpdate("TRUNCATE " + tableName);
connection.commit();
return result;
}
TRUNCATE对于表中的数据是事务安全的:如果周围的事务没有提交,截断将被安全地回滚。
答案 1 :(得分:2)
如果表具有依赖关系,则可能会遇到问题。如果是这样,首先截断父表,并使用CASCADE选项。
Connection connection = getConnection();
try {
PreparedStatement statement = connection.prepareStatement("TRUNCATE " + parentTable1, parentTable2, ... + " CASCADE");
try {
return statement.execute();
} finally {
statement.close();
}
} finally {
connection.close();
}
答案 2 :(得分:0)
首先,如果要截断一个表,您可能还希望重新启动身份(除了可能进行CASCADE之外,如John Hogan所述)。
第二,就进行connection.commit()而言,假设您已将自动提交设置为OFF。我的Postgres设置为ON(显然,这有时是默认设置)。 如果将其设置为ON,则不需要调用提交,这将导致错误: “ org.postgresql.util.PSQLException:启用autoCommit后无法提交。”
第三,您可能无权截断表(或重新启动标识)。在这种情况下,您将需要:
DELETE from your_table
SELECT setval('your_table_id', 1)
以下对我有用:
public String truncateTable(String tableName, boolean cascadeFlag) {
String message = "";
try {
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String truncation = "TRUNCATE TABLE yourSchema." + tableName + " RESTART IDENTITY" + (cascadeFlag ? " CASCADE" : "");
System.out.println("truncateTable: Executing query '" + truncation + "'.");
int result = statement.executeUpdate(truncation);
// connection.commit(); // If autocommit is enabled (which it is for our DB), then throws exception after truncating the table.
statement.close();
connection.close();
} catch (SQLException sqlex) {
message = "Could not truncate table " + tableName + ". " + sqlex.getMessage();
System.err.println(message);
sqlex.printStackTrace();
}
return message;
}
也:
public int deleteResetTable(String tableName, String fieldName) {
int affectedRows = 0;
try {
connection = DriverManager.getConnection(url, username, password);
String sql = "DELETE FROM yourSchema." + tableName;
PreparedStatement preparedStatement = connection.prepareStatement(sql);
affectedRows = preparedStatement.executeUpdate();
System.out.println("Deleted " + affectedRows+ " rows from table " + tableName + ".");
sql = "SELECT setval('yourSchema." + fieldName + "', 1)";
preparedStatement = connection.prepareStatement(sql);
affectedRows = preparedStatement.executeUpdate();
System.out.println("Reset " + affectedRows+ " values from table " + tableName + ".");
} catch (SQLException ex) {
System.out.println("Failed to delete rows from " + tableName + " " + ex.getMessage());
}
return affectedRows;
}