在JDBC的API中executeQuery()和execute()、executeUpdate()的区别。

解决方案 »

  1.   

    我觉得楼主可以查一下API上解释,如果再不明白再来问也不迟的
      

  2.   

    顶楼上。查API很重要也很有用
      

  3.   

    executeQuery
    ResultSet executeQuery()
                           throws SQLException
    Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query. Returns:
    a ResultSet object that contains the data produced by the query; never null 
    Throws: 
    SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement does not return a ResultSet objectexecute
    public void execute()
                 throws Exception
    The execute method finds a method whose name is the same as the methodName property, and invokes the method on the target. When the target's class defines many methods with the given name the implementation should choose the most specific method using the algorithm specified in the Java Language Specification (15.11). The dynamic class of the target and arguments are used in place of the compile-time type information and, like the java.lang.reflect.Method class itself, conversion between primitive values and their associated wrapper classes is handled internally. 
    The following method types are handled as special cases: Static methods may be called by using a class object as the target. 
    The reserved method name "new" may be used to call a class's constructor as if all classes defined static "new" methods. Constructor invocations are typically considered Expressions rather than Statements as they return a value. 
    The method names "get" and "set" defined in the java.util.List interface may also be applied to array instances, mapping to the static methods of the same name in the Array class. Throws: 
    ExceptionexecuteUpdate
    int executeUpdate()
                      throws SQLException
    Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement. Returns:
    either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing 
    Throws: 
    SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement returns a ResultSet object
      

  4.   

    方法executeQuery
       用于产生单个结果集的语句,例如 SELECT 语句。 被使用最多的执行 SQL 语句的方法是 executeQuery。这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL 语句。
      
      方法executeUpdate
       用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。
      
       使用executeUpdate方法是因为在 createTableCoffees 中的 SQL 语句是 DDL (数据定义语言)语句。创建表,改变表,删除表都是 DDL 语句的例子,要用 executeUpdate 方法来执行。你也可以从它的名字里看出,方法 executeUpdate 也被用于执行更新表 SQL 语句。实际上,相对于创建表来说,executeUpdate 用于更新表的时间更多,因为表只需要创建一次,但经常被更新。
      
      
      方法execute
       用于执行返回多个结果集、多个更新计数或二者组合的语句。因为多数程序员不会需要该高级功能
      
       execute方法应该仅在语句能返回多个ResultSet对象、多个更新计数或ResultSet对象与更新计数的组合时使用。当执行某个已存储过程 或动态执行未知 SQL 字符串(即应用程序程序员在编译时未知)时,有可能出现多个结果的情况,尽管这种情况很少见。
       因为方法 execute 处理非常规情况,所以获取其结果需要一些特殊处理并不足为怪。例如,假定已知某个过程返回两个结果集,则在使用方法 execute 执行该过程后,必须调用方法 getResultSet 获得第一个结果集,然后调用适当的 getXXX 方法获取其中的值。要获得第二个结果集,需要先调用 getMoreResults 方法,然后再调用 getResultSet 方法。如果已知某个过程返回两个更新计数,则首先调用方法 getUpdateCount,然后调用 getMoreResults,并再次调用 getUpdateCount。
       对于不知道返回内容,则情况更为复杂。如果结果是 ResultSet 对象,则方法 execute 返回 true;如果结果是 Java int,则返回 false。如果返回 int,则意味着结果是更新计数或执行的语句是 DDL 命令。在调用方法 execute 之后要做的第一件事情是调用 getResultSet 或 getUpdateCount。调用方法 getResultSet 可以获得两个或多个 ResultSet 对象中第一个对象;或调用方法 getUpdateCount 可以获得两个或多个更新计数中第一个更新计数的内容。
    详细介绍