JDBC on executeQuery, executeUpdate and execute

The Statement interface provides three methods for executing SQL statements: executeQuery, executeUpdate, and execute. Which method is used is determined by what is produced by the SQL statement. 

★Method executeQuery -- a single result set (commonly used) 
is used to generate a single result set statement, 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 return value of --executeUpdate is an integer indicating the number of rows affected (ie, 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 (less used)  --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 executing a stored procedure or executing an unknown SQL string dynamically (that is, unknown to the application programmer at compile time), it is possible, although rare, to have multiple results. Data: Detailed explanation of execute method  ★Method execute: --Used  to execute statements that return multiple result sets, multiple update counts, or a combination of the two. 
   






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 executing a stored procedure or executing an unknown SQL string dynamically (that is, unknown to the application programmer at compile time), it is possible, although rare, to have multiple results. 
-- Because the method execute handles unusual cases, it's not surprising that getting its result requires some special handling. For example, given that a procedure is known to return two result sets, after executing the procedure using the method execute, the method getResultSet must be called to obtain the first result set, and then the appropriate getXXX methods must be called to obtain the values ​​in it. To get the second result set, 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, and getUpdateCount again. 
For not knowing what to return, the situation is more complicated. The method execute returns true if the result is a ResultSet object; false if the result is a Java int. If it returns an int, it means 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 method getResultSet to get the first of two or more ResultSet objects; or call the method getUpdateCount to get the contents of the first update count of two or more update counts. 
--当 SQL 语句的结果不是结果集时,则方法 getResultSet 将返回 null。这可能意味着结果是一个更新计数或没有其它结果。在这种情况下,判断 null 真正含义的唯一方法是调用方法 getUpdateCount,它将返回一个整数。这个整数为调用语句所影响的行数;如果为 -1 则表示结果是结果集或没有结果。如果方法 getResultSet 已返回 null(表示结果不是 ResultSet 对象),则返回值 -1 表示没有其它结果。也就是说,当下列条件为真时表示没有结果(或没有其它结果): 

((stmt.getResultSet() == null) && (stmt.getUpdateCount() == -1)) 

如果已经调用方法 getResultSet 并处理了它返回的 ResultSet 对象,则有必要调用方法 getMoreResults 以确定是否有其它结果集或更新计数。如果 getMoreResults 返回 true,则需要再次调用 getResultSet 来检索下一个结果集。如上所述,如果 getResultSet 返回 null,则需要调用 getUpdateCount 来检查 null 是表示结果为更新计数还是表示没有其它结果。 

当 getMoreResults 返回 false 时,它表示该 SQL 语句返回一个更新计数或没有其它结果。因此需要调用方法 getUpdateCount 来检查它是哪一种情况。在这种情况下,当下列条件为真时表示没有其它结果: 

((stmt.getMoreResults() == false) && (stmt.getUpdateCount() == -1)) 

下面的代码演示了一种方法用来确认已访问调用方法 execute 所产生的全部结果集和更新计数: 


stmt.execute(queryStringWithUnknownResults); 
while (true) { 
int rowCount = stmt.getUpdateCount(); 
if (rowCount > 0) { // 它是更新计数 
System.out.println("Rows changed = " + count); 
stmt.getMoreResults(); 
continue; 

if (rowCount == 0) { // DDL 命令或 0 个更新 
System.out.println(" No rows changed or statement was DDL 
command"); 
stmt.getMoreResults(); 
continue; 


// 执行到这里,证明有一个结果集 
// 或没有其它结果 

ResultSet rs = stmt.getResultSet; 
if (rs != null) { 
. . . // 使用元数据获得关于结果集列的信息 
while (rs.next()) { 
. . . // 处理结果 
stmt.getMoreResults(); 
continue; 

break; // 没有其它结果 

 

 

FROM:http://ldison.iteye.com/blog/1112706

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481072&siteId=291194637