我想在使用JDBC的Oracle 11g數據庫上執行DDL語句。我正在使用Statement
類的boolean execute(String SQL)
。在JDBC中,如何知道DDL語句是否成功執行?
以下是代碼片段執行查詢,並試圖確定查詢結果:
// Create a SQL string
String dropSql = "DROP TABLE Reviews";
stmt = conn.createStatement();
// Execute the query
boolean result = stmt.execute(dropSql);
// Get the result of the drop operation
if(result)
{
// Its a result set
System.out.println("Atleast one result set has been returned. Loop through them");
}
else
{
// Its an update count or no result
Integer updateCnt = stmt.getUpdateCount();
if(updateCnt == -1)
{
// No results
System.out.println("No results returned by the query");
}
else
{
// Update Count
System.out.println("Update Count: " + updateCnt);
}
}
我不是一個數據庫的傢伙,但有一種情況:DDL語句將無法執行而不是提出SQLException
?如果沒有,那麼我不需要捕獲執行方法返回的內容。如果缺少SQLException
,則表示DDL語句成功執行。
,我下面的tutorial recommendeds對DDL語句的使用這種方法:
boolean execute(String SQL) : ....... Use this method to execute SQL DDL statements or when you need to use truly dynamic SQL.
雖然Jdbc documentation建議執行DDL語句使用int executeUpdate(String sql)
。兩者中哪一個更受歡迎?