The difference between execute, ExecuteQuery, and executeupdate in MySQL

Source: Internet
Author: User
Tags rowcount

In the use of pure JSP to do a page alarm function, the habitual use of executequery to execute SQL statements, the results of the implementation of the update encountered a problem, the statement can be executed, but the result of the return problem, Also ignore the executeupdate return value is not the result set resultset, but the value! Special collection of the following article:
The difference between execute, ExecuteQuery, and executeupdate provided by the statement interface in JDBCTM
The Statement interface provides three ways to execute SQL statements: ExecuteQuery, Executeupdate, and execute. Which method to use is determined by the content produced by the SQL statement.
The ExecuteQuery method uses statements that produce a single result set, such as a SELECT statement. The most used method of executing SQL statements is executeQuery. This method is used to execute the SELECT statement, which is almost the most frequently used SQL statement.
The Executeupdate method is used to execute INSERT, UPDATE, or DELETE statements, as well as SQL DDL (data definition language) statements, such as CREATE table and DROP table. The effect of an INSERT, UPDATE, or DELETE statement is to modify one or more columns in 0 or more rows in a table. The return value of Executeupdate is an integer that indicates the number of rows affected (that is, the update count). For statements that do not manipulate rows such as CREATE table or DROP table, the return value of executeupdate is always zero.
The Executeupdate method is used because the SQL statement in Createtablecoffees is a DDL (data definition Language) statement. Creating tables, altering tables, and deleting tables are examples of DDL statements that are executed using the Executeupdate method. You can also see from its name that method Executeupdate is also used to execute an UPDATE table SQL statement. In fact, Executeupdate is more time-dependent for updating tables than creating tables, because tables need to be created only once, but are often updated.

Method execute: Used to perform statements that return multiple result sets, multiple update counts, or a combination of both. Because most programmers don't need this advanced functionality
The Execute method should only be used when the statement can return multiple ResultSet objects, multiple update counts, or a combination of ResultSet objects and update counts. When a stored procedure is executed or an unknown SQL string is executed dynamically (that is, the application programmer is not known at compile time), there is a chance that multiple results may occur, although this is rare. Because the method execute handles unconventional situations, it is not surprising to get its results with some special handling. For example, assuming that a procedure is known to return two result sets, you must call the method Getresultset to get the first result set after you execute the procedure using method execute, and then call the appropriate GetXXX method to get the values in it. To get the second result set, you need to call the Getmoreresults method before calling the Getresultset method. If a procedure is known to return two update counts, the method Getupdatecount is called first, then Getmoreresults is called, and Getupdatecount is called again. The situation is more complex for those who do not know what to return. If the result is a ResultSet object, the method execute returns TRUE, or False if the result is a Java int. If an int is returned, it means that the result is an update count or the statement executed is a DDL command. The first thing to do after calling the method execute is to call Getresultset or Getupdatecount. Call method Getresultset to get the first object in two or more ResultSet objects, or call the method Getupdatecount to get the contents of the first update count in two or more update counts. When the result of the SQL statement is not a result set, the method getresultset returns NULL. This may mean that the result is an update count or no other result. In this case, the only way to judge the true meaning of NULL is to call the method Getupdatecount, which returns an integer. This integer is the number of rows affected by the invocation statement, and if 1 indicates that the result is a result set or no result. If the method Getresultset has returned null (indicating that the result is not a ResultSet object), the return value of 1 means that there is no other result. That is, when the following conditions are true, there is no result (or no other result):
((stmt.getresultset () = = null) && (stmt.getupdatecount () = =-1))
If the method Getresultset has been called and the ResultSet object it returns is processed, it is necessary to call the method getmoreresults to determine whether there are additional result sets or update counts. If Getmoreresults returns True, you will need to call Getresultset again to retrieve the next result set. As mentioned above, if Getresultset returns NULL, you need to call Getupdatecount to check whether NULL indicates whether the result is an update count or no other result.
When Getmoreresults returns FALSE, it indicates that the SQL statement returned an update count or no other result. Therefore, you need to call method Getupdatecount to check which case it is. In this case, there is no other result when the following conditions are true:
((stmt.getmoreresults () = = False) && (stmt.getupdatecount () = =-1))
The following code demonstrates a method used to confirm all result sets and update counts resulting from access to the calling method execute:

Stmt.execute (Querystringwithunknownresults); while (true) {int rowCount = Stmt.getupdatecount (); if (RowCount > 0) {// It is the update Count System.out.println ("Rows changed =" + count); Stmt.getmoreresults (); continue;} if (RowCount = = 0) {//DDL command or 0 update System.out.println ("No rows changed or statement was Ddlcommand"); Stmt.getmoreresult s (); continue;}
Executed here, proving that there is a result set//or no other result
ResultSet rs = stmt.getresultset;if (rs! = null) {...//use metadata to get information about the result set column while (Rs.next ()) {...//processing result stmt.getmoreres Ults (); continue;} Break No other results

The difference between execute, ExecuteQuery, and executeupdate in MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.