gpt4 book ai didi

java - 如何在Java语句中使用execute()获取true或false

转载 作者:行者123 更新时间:2023-12-01 06:43:13 25 4
gpt4 key购买 nike

我有名为stmtStatement对象,连接对象conn

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

但这总是会产生错误。如果上述查询执行成功,我想要 true ;如果查询执行失败,我想要 false 。如何使用 execute() 方法实现该结果。

最佳答案

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

你不能。如果结果有 ResultSet,它只会返回 true。如果您的插入出现问题,该方法将抛出异常。来自 documentation :

boolean execute(String sql) throws SQLException

Returns:

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

Throws:

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 - 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 - 如何在Java语句中使用execute()获取true或false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29521554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com