怎样判断java的statement执行create table 等ddl时是否成功

来源:百度知道 编辑:UC知道 时间:2024/06/17 01:40:25
java statement的executeupdate()方法在执行创建表,删除表等操作时就算没有创建成功也不会报错,有什么办法判断是否创建成功???

try{
statement.executeUpdate();
}catch(SQLException e){
System.out.println(e.getMessage()); //如果不想在控制台输出可以换成其他的流
}

java 的api是这么写的
executeUpdate
int executeUpdate(String sql)
throws SQLExceptionExecutes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

Returns:
either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing

它的返回值:如果是dml,返回影响的行数,如果是ddl,返回0.

但是它会抛异常的啊,所以如果删除/创建失败,它就会抛一个 SQLException 的异常,你要捕获并处理这个异常!