我有一个Postgresql数据库,我想用JDBC截断一些表。我怎么做?如何从JDBC中截断Postgresql的表
这是我尝试过,但没有工作......没有被报告甚至没有任何错误:
使用CallableStatement
。
try (Connection connection = getConnection();
CallableStatement statement = connection.prepareCall("TRUNCATE " + tableName)) {
return statement.execute();
}
使用Statement
。使用PreparedStatement
。
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement("TRUNCATE " + tableName)) {
return statement.execute();
}
'CallableStatement'用于调用DB中的存储过程。 'TRUNCATE'不是SP,因此它不应该工作。当你使用普通的'Statement'时,你会得到什么错误?您还可以启用“JDBC驱动程序”跟踪来检查里面发生了什么。 –
从未报告过任何错误。对于任何尝试。我如何启用JDBC跟踪? –
看起来您的错误在客户端代码的某个地方变得糟糕。在最后加上一个catch(Exception e){e.printStacktrace();}'块。您可以通过遵循'DriverManager.setLogWriter()'API启用跟踪或参考实际的驱动程序指南。您正在使用什么JDBC驱动程序 –