简体   繁体   English

Statement.execute(sql) 与 executeUpdate(sql) 和 executeQuery(sql)

[英]Statement.execute(sql) vs executeUpdate(sql) and executeQuery(sql)

I have a question related to this method: st.execute(sql);我有一个与此方法相关的问题: st.execute(sql); where st obviously is a Statement object.其中 st 显然是一个 Statement 对象。 Directly from this oracle java tutorial:直接来自这个oracle java教程:

execute: Returns true if the first object that the query returns is a ResultSet object. execute:如果查询返回的第一个对象是 ResultSet 对象,则返回 true。 Use this method if the query could return one or more ResultSet objects.如果查询可以返回一个或多个 ResultSet 对象,请使用此方法。 Retrieve the ResultSet objects returned from the query by repeatedly calling Statement.getResutSet.通过重复调用 Statement.getResutSet 来检索从查询返回的 ResultSet 对象。

What is meant by " one or more ResultSet objects "? 一个或多个 ResultSet 对象”是什么意思? How is it possible to manage them once got an array of ResultSet ?一旦获得了ResultSet数组,如何管理它们? Whereas st.executeQuery(sql) and st.executeUpdate(sql) are very clear.st.executeQuery(sql)st.executeUpdate(sql)则非常清楚。 It's not (at least to me) the aim of st.execute(sql) which can also return an int as if it was updated a table.这不是(至少对我而言) st.execute(sql) ) 的目标,它也可以返回一个 int ,就好像它更新了一个表一样。

Thanks in advance提前致谢

boolean execute(): Executes the SQL statement in this Prepared Statement object, which may be any kind of SQL statement. boolean execute():执行这个Prepared Statement对象中的SQL语句,可以是任何一种SQL语句。

ResultSet executeQuery(): Executes the SQL query in this Prepared Statement object and returns the ResultSet object generated by the query. ResultSet executeQuery():在这个Prepared Statement对象中执行SQL查询,返回查询生成的ResultSet对象。

int executeUpdate(): Executes the SQL statement in this Prepared Statement object, which must be an SQL INSERT, UPDATE or DELETE statement; int executeUpdate():执行这个Prepared Statement对象中的SQL语句,必须是SQL INSERT、UPDATE或DELETE语句; or an SQL statement that returns nothing, such as a DDL statement.或不返回任何内容的 SQL 语句,例如 DDL 语句。

The best place to find answers to questions like this is the Javadocs: Here寻找此类问题答案的最佳地点是 Javadocs: 这里

What do they mean by "one or more ResultSet objects"? “一个或多个 ResultSet 对象”是什么意思?

The javadoc for the execute method says this: execute方法的 javadoc 是这样说的:

" Executes the given SQL statement, which may return multiple results. In some (uncommon) situations, a single SQL statement may return multiple result sets and/or update counts. Normally you can ignore this unless you are (1) executing a stored procedure that you know may return multiple results or (2) you are dynamically executing an unknown SQL string. " "执行给定的 SQL 语句,它可能返回多个结果。在某些(不常见的)情况下,单个 SQL 语句可能返回多个结果集和/或更新计数。通常您可以忽略这一点,除非您 (1) 执行存储过程您知道可能会返回多个结果或 (2) 您正在动态执行未知的 SQL 字符串。

That pretty much explains it.这几乎解释了它。 Sometimes a query can deliver more than one ResultSet .有时一个查询可以提供多个ResultSet

if so how is it possible to manage them once got an array of ResultSet?如果是这样,一旦获得了 ResultSet 数组,如何管理它们?

I'm not sure what you mean but:我不确定你的意思,但是:

  • you can't get them as an array: you must get them one at a time, and您不能将它们作为数组获取:您必须一次获取一个,并且
  • you could put the ResultSets into an array ...您可以将 ResultSets 放入一个数组中...

It's not (at least to me) the aim of st.execute(sql) which can also return an int as if it was updated a table.这不是(至少对我而言) st.execute(sql) 的目标,它也可以返回一个 int ,就好像它更新了一个表一样。

One use of execute is to execute an SQL statement if you don't know if it is a query, an update (of some kind) ... or something else that potentially delivers multiple result sets. execute一个用途是执行 SQL 语句,如果您不知道它是查询、更新(某种类型的)……或其他可能提供多个结果集的东西。 It is a generalization of executeQuery() and executeUpdate() ...它是executeQuery()executeUpdate()的概括......

execute() : The method used for all types of SQL statements, and that is, returns a boolean value of TRUE or FALSE. execute() :用于所有类型的 SQL 语句的方法,即返回布尔值 TRUE 或 FALSE。 If the method return TRUE, return the ResultSet object and FALSE returns the int value.如果该方法返回 TRUE,则返回 ResultSet 对象,FALSE 返回 int 值。

executeUpdate() : This method is used for execution of DML statement(INSERT, UPDATE and DELETE) which is return int value, count of the affected rows. executeUpdate() :此方法用于执行 DML 语句(INSERT、UPDATE 和 DELETE),该语句返回 int 值,受影响行的计数。

executeQuery() : This method is used to retrieve data from database using SELECT query. executeQuery() :此方法用于使用 SELECT 查询从数据库中检索数据。 This method returns the ResultSet object that returns the data according to the query.此方法返回根据查询返回数据的 ResultSet 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM