![](/img/trans.png)
[英]Getting column metadata from jdbc/postgresql for newly created table
[英]How to truncate a Postgresql's table from JDBC
我有一個 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();
}
截斷后,我需要提交:
try (Connection connection = getConnection();
Statement statement = connection.createStatement()) {
int result = statement.executeUpdate("TRUNCATE " + tableName);
connection.commit();
return result;
}
從文檔:
TRUNCATE 對於表中的數據是事務安全的:如果周圍的事務沒有提交,截斷將安全回滾。
如果表有依賴關系,您可能會遇到問題。 如果是這樣,請先截斷父表,並使用 CASCADE 選項。
Connection connection = getConnection();
try {
PreparedStatement statement = connection.prepareStatement("TRUNCATE " + parentTable1, parentTable2, ... + " CASCADE");
try {
return statement.execute();
} finally {
statement.close();
}
} finally {
connection.close();
}
首先,如果您要截斷一個表,您可能還想重新啟動 IDENTITY(除了可能執行 CASCADE,正如 John Hogan 提到的那樣)。
其次,就執行 connection.commit() 而言,假設您已將自動提交設置為 OFF。 我的 Postgres 設置為 ON(顯然,有時這是默認設置)。 如果設置為ON,則不需要調用提交,並會導致錯誤:“org.postgresql.util.PSQLException:啟用自動提交時無法提交。”
第三,您可能沒有截斷表(或重新啟動身份)的權限。 在這種情況下,您需要:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.