java 如何在Java语句中使用execute()获取真假

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29521554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:25:04  来源:igfitidea点击:

how to get true or false using execute() in Java Statement

javaexecute

提问by Shavindra Manathunga

I have Statementobject called stmt, Connection object conn.

我有名为stmt 的Statement对象, Connection 对象conn

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

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

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

回答by Anderson Vieira

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

如何使用 execute() 方法实现该结果。

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

你不能。仅true当结果具有ResultSet. 如果您的插入有问题,该方法将抛出异常。从文档

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

布尔执行(字符串 sql)抛出 SQLException

返回:

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

抛出:

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

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

回答by Yogesh_D

Try using executeUpdate()method on the Statement. It should return you a int indicating the number of rows.

尝试使用的executeUpdate()方法的Statement。它应该返回一个 int 指示行数。

回答by silentprogrammer

Statement.ececute()will return true if the first result is a ResultSet object, false if it is an update count or there are no results

Statement.ececute()如果第一个结果是 ResultSet 对象,则返回 true,如果是更新计数或没有结果,则返回 false

You can use

您可以使用

int executeUpdate(String sql)

int executeUpdate(String sql)

returns

回报

1) the row count for SQL Data Manipulation Language (DML) statements or

2) 0 for SQL statements that return nothing

1) SQL 数据操作语言 (DML) 语句的行数或

2) 0 表示不返回任何内容的 SQL 语句

Docs

文档

回答by Saif

You cant if it is not a select query.
This is what said in the doc

如果它不是选择查询,则不能。
这是文档中所说的

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

You will get a result set when you give a select query not when insert or update, you will get false always in case of these type of query .
Now to make it sure if its run successfully or not i guess working with e Exceptionswill help you

当您给出选择查询而不是插入或更新时,您将获得一个结果集,在这些类型的查询的情况下,您总是会得到 false。
现在要确定它是否成功运行,我想与 e 一起工作Exceptions会帮助你

Using executeUpdate()will give you the row count so you can use that to predict.

使用executeUpdate()将为您提供行数,以便您可以使用它进行预测。

回答by sathish_at_madison

You are doing an insert statement, If there is an resultset that has values returning from the execute statement there will be a result. Check the documentation

您正在执行插入语句,如果结果集具有从执行语句返回的值,则会有结果。检查文档