The statement interface provides three methods for executing SQL statements: executequery, executeupdate, and execute. The method used is determined by the content generated by the SQL statement.
★Method executequery-single result set (commonly used)
A statement used to generate a single result set, such as a SELECT statement.
★Method executeupdate -- (non-select statement)
-- Used to execute all other statements that are not select statements, such as insert, update or delete, create table, and drop table. -- The returned value of executeupdate is an integer indicating the number of affected rows (that is, the update count ). For statements that do not operate on rows such as create table or drop table, the return value of executeupdate is always zero.
Note: In JDBC development, the above two methods are enough.
★Method execute -- multiple result sets (rarely used)
-- Execute should be used only when the statement can return multiple resultset objects, multiple update counts, or a combination of resultset objects and update counts. When you execute a stored procedure or dynamically execute an unknown SQL string (that is, the application programmer is unknown during compilation), multiple results may occur, although this situation is rare.
Details: Execute Method
★Method execute:
-- Runs the statement that returns multiple result sets, multiple update counts, or a combination of the two.
-- Execute should be used only when the statement can return multiple resultset objects, multiple update counts, or a combination of resultset objects and update counts. When you execute a stored procedure or dynamically execute an unknown SQL string (that is, the application programmer is unknown during compilation), multiple results may occur, although this situation is rare.
-- Because the method execute handles Unconventional situations, it takes some special processing to obtain the results and it is not surprising. For example, if two result sets are returned in a known process, you must call the getresultset method to obtain the first result set after executing the process using method execute, call the appropriate getxxx method to obtain the value. To obtain the second result set, call the getmoreresults method and then call the getresultset method. If two update counts are returned for a specific process, call the getupdatecount method, call getmoreresults, and call getupdatecount again.
If you do not know the returned content, the situation is more complicated. If the result is a resultset object, the method execute returns true; if the result is a Java int, false is returned. If int is returned, it means that the result is an update count or the executed statement is a DDL command. The first thing to do after calling the method execute is to call getresultset or getupdatecount. Call the getresultset method to obtain the first object of two or more resultset objects, or call the getupdatecount method to obtain the first update count of two or more update counts.
-- If the SQL statement result is not a result set, the getresultset method returns NULL. This may mean that the result is an update count or no other results. In this case, the only way to determine the true meaning of null is to call the getupdatecount method, which returns an integer. This integer is the number of rows affected by the call statement. If it is-1, 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-1 indicates that no other results are returned. That is, when the following conditions are true, no results (or no other results) are returned ):
(Stmt. getresultset () = NULL) & (stmt. getupdatecount () =-1 ))
If you have already called the getresultset method and processed the resultset object returned by it, it is necessary to call the getmoreresults method to determine whether there are other result sets or update counts. If getmoreresults returns true, 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 the result is an update count or no other results.
When getmoreresults returns false, it indicates that the SQL statement returns an update count or no other results. Therefore, you need to call the getupdatecount method to check which situation it is. In this case, when the following conditions are true, no other results are returned:
(Stmt. getmoreresults () = false) & (stmt. getupdatecount () =-1 ))
The following code demonstrates a method to confirm all the result sets and update counts generated by the Access Call method execute:
Stmt.exe cute (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 updates
System. Out. println ("No rows changed or statement was DDL
Command ");
Stmt. getmoreresults ();
Continue;
}
// Execute here to prove that there is a result set
// Or no other results
Resultset rs = stmt. getresultset;
If (RS! = NULL ){
... // Use metadata to obtain information about the result set Column
While (Rs. Next ()){
... // Processing result
Stmt. getmoreresults ();
Continue;
}
Break; // no other results