Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statement.execute(sql) vs executeUpdate(sql) and executeQuery(sql)

I have a question related to this method: st.execute(sql); where st obviously is a Statement object. Directly from this oracle java tutorial:

execute: Returns true if the first object that the query returns is a ResultSet object. Use this method if the query could return one or more ResultSet objects. Retrieve the ResultSet objects returned from the query by repeatedly calling Statement.getResutSet.

What is meant by "one or more ResultSet objects"? How is it possible to manage them once got an array of ResultSet? Whereas st.executeQuery(sql) and st.executeUpdate(sql) are very clear. 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.

Thanks in advance

like image 572
Rollerball Avatar asked May 18 '13 15:05

Rollerball


People also ask

What is difference between executeQuery and execute?

execute method can run both select and insert/update statements. executeQuery method execute statements that returns a result set by fetching some data from the database. It executes only select statements. executeUpdate method execute sql statements that insert/update/delete data at the database.

What is the difference between STMT executeQuery and STMT executeUpdate?

executeQuery() command used for getting the data from database whereas executeUpdate() command used for insert,update,delete or execute() command used forany kind of operations.

What is executeQuery in SQL?

executeQuery : Returns one ResultSet object. executeUpdate : Returns an integer representing the number of rows affected by the SQL statement. Use this method if you are using INSERT , DELETE , or UPDATE SQL statements.

Which specified query is executed by executeUpdate () method?

2) public int executeUpdate(String sql): is used to execute specified query, it may be create, drop, insert, update, delete etc.


3 Answers

boolean execute(): Executes the SQL statement in this Prepared Statement object, which may be any kind of SQL statement.

ResultSet executeQuery(): Executes the SQL query in this Prepared Statement object and returns the ResultSet object generated by the query.

int executeUpdate(): Executes the SQL statement in this Prepared Statement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.

The best place to find answers to questions like this is the Javadocs: Here

like image 170
Kaveh Safavi Avatar answered Oct 12 '22 10:10

Kaveh Safavi


What do they mean by "one or more ResultSet objects"?

The javadoc for the execute method says this:

"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."

That pretty much explains it. Sometimes a query can deliver more than one ResultSet.

if so how is it possible to manage them once got an array of 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 ...

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.

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. It is a generalization of executeQuery() and executeUpdate() ...

like image 41
Stephen C Avatar answered Oct 12 '22 11:10

Stephen C


execute() : The method used for all types of SQL statements, and that is, returns a boolean value of TRUE or FALSE. If the method return TRUE, return the ResultSet object and FALSE returns the int value.

executeUpdate() : This method is used for execution of DML statement(INSERT, UPDATE and DELETE) which is return int value, count of the affected rows.

executeQuery() : This method is used to retrieve data from database using SELECT query. This method returns the ResultSet object that returns the data according to the query.

like image 43
satish kendre Avatar answered Oct 12 '22 12:10

satish kendre