如何在Java语句中使用execute()获得true或false [英] how to get true or false using execute() in Java Statement

查看:120
本文介绍了如何在Java语句中使用execute()获得true或false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 stmt Statement 对象,一个连接对象 conn .

I have Statement object called stmt, Connection object conn.

stmt = conn.createStatement();
boolean b = stmt.execute("INSERT INTO employee VALUES('E001', 'Smith')")

但这总是产生 false .如果上述查询成功执行,我要 true ,如果查询执行失败,我要 false .如何使用 execute()方法获得该结果.

But this always produce false. I want true if above query executed successfully and false if the query fails executing. How can I achieve that result using execute() method.

推荐答案

如何使用execute()方法获得该结果.

How can I achieve that result using execute() method.

不能.如果结果为ResultSet,则仅返回true.如果您的插入有问题,该方法将引发异常.来自文档:

You can't. It will only return true if the result has a ResultSet. If there is a problem with your insertion, the method will throw an exception. From the documentation:

布尔执行(String sql)抛出SQLException

boolean execute(String sql) throws SQLException

返回:

如果第一个结果是ResultSet对象,则为true ;如果是更新计数或没有结果,则为false

true if the first result is a ResultSet object; false if it is an update count or there are no results

投掷:

SQLException-如果发生数据库访问错误,则在封闭的Statement上调用此方法,在PreparedStatement或CallableStatement上调用该方法

SQLException - if a database access error occurs, this method is called on a closed Statement, the method is called on a PreparedStatement or CallableStatement

SQLTimeoutException-当驱动程序确定已经超过了setQueryTimeout方法指定的超时值,并且至少尝试取消当前正在运行的Statement

SQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement

这篇关于如何在Java语句中使用execute()获得true或false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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