我有一个Postgresql数据库,希望使用JDBC截断一些表。我该怎么做?
这是我的尝试,但都失败了...甚至连错误提示都没有:
使用CallableStatement。
使用
这是我的尝试,但都失败了...甚至连错误提示都没有:
使用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();
}
catch(Exception e) {e.printStacktrace();}
块。你可以通过使用DriverManager.setLogWriter()
API启用跟踪,或者参考实际的驱动程序指南。你正在使用哪个JDBC驱动程序? - Bimalesh Jhaboolean result = statement.execute("TRUNCATE " + tableName); System.out.println("Everything is ok"); return result;
,然后我在控制台上看到了“Everything is ok”。 感谢您提供有关logwriter信息的信息。我正在使用驱动程序org.postgresql:postgresql:9.3-1101-jdbc4
(从Maven检索)。 现在你正在关注错误的部分。请不要告诉我我做错了什么,请告诉我如何正确地做,以及您自己如何做。 - Olivier GrégoireStatement.executeUpdate
方法,而不是execute
。 - krokodilkocatch
部分,会忽略所有错误,你必须捕获异常并检查错误代码/消息。 - krokodilko