在JDBC中,如何知道DDL语句是否成功执行? [英] In JDBC how does one know if the DDL statement was executed successfully?

查看:674
本文介绍了在JDBC中,如何知道DDL语句是否成功执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JDBC在Oracle 11g数据库上执行DDL语句.我正在使用Statement类的boolean execute(String SQL)进行此操作.

I am trying to execute a DDL statement on a Oracle 11g database using JDBC. I am doing it using the boolean execute(String SQL) of the Statement class.

以下是执行查询并尝试确定查询结果的代码段:

Following is the code snippet that executes the query and tries to determine the result of the query:

// 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的情况?如果没有,则无需捕获execute方法返回的内容.缺少SQLException表示DDL语句已成功执行.

I am not a database guy, but is there a situation where the DDL statement would fail to execute and not raise a SQLException? If there isn't then I do not need to capture what the execute method returns. An absence of SQLException would indicate that the DDL statements executed successfully.

我关注的教程建议对DDL使用此方法声明:

The tutorial that I am following recommendeds the use of this method for DDL statements:

boolean execute(String SQL) : ....... Use this method to execute SQL DDL statements or when you need to use truly dynamic SQL. 

Jdbc文档建议使用int executeUpdate(String sql)执行DDL语句.两者中哪一个是更可取的?

While the Jdbc documentation recommends the use of int executeUpdate(String sql) for executing DDL statements. Which one is preferred among the two ?

推荐答案

一条失败的语句将始终引发SQLException,无论使用哪种方法执行它. executeUpdate适用于任何不能生成ResultSet的语句,这使其非常适合DDL语句.

A failing statement will always raise an SQLException regardless of which method is used to execute it. executeUpdate is suitable for any statement that cannot produce a ResultSet, which makes it fit the bill perfectly for a DDL statement.

这篇关于在JDBC中,如何知道DDL语句是否成功执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆