各位大虾:
   sql="delete from tbl_test where mytype=?";
   PreparedStatement ps= conn.prepareStatement(sql);
   ps.setString(1,"abc");
   ps.addBatch();
   
   int a=   ps.executeBatch();
 
   我的delete from tbl_test where mytype='abc'应该有5条数据被删除,可为何
此处 a.length 的值确为 1
   executeBatch()返回的不是实际执行语句的结果吗?   谢谢
   

解决方案 »

  1.   

    executeBatch 返回的是一个数组
    每个元素才是对应命令update记录 的数量
      

  2.   

    你只执行了一句sql,数组长度,自然是1
      

  3.   

    executeBatch
    int[] executeBatch()
                       throws SQLException
    Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch. The elements in the array returned by the method executeBatch may be one of the following: 
    A number greater than or equal to zero -- indicates that the command was processed successfully and is an update count giving the number of rows in the database that were affected by the command's execution 
    A value of SUCCESS_NO_INFO -- indicates that the command was processed successfully but that the number of rows affected is unknown 
    If one of the commands in a batch update fails to execute properly, this method throws a BatchUpdateException, and a JDBC driver may or may not continue to process the remaining commands in the batch. However, the driver's behavior must be consistent with a particular DBMS, either always continuing to process commands or never continuing to process commands. If the driver continues processing after a failure, the array returned by the method BatchUpdateException.getUpdateCounts will contain as many elements as there are commands in the batch, and at least one of the elements will be the following: 
    A value of EXECUTE_FAILED -- indicates that the command failed to execute successfully and occurs only if a driver continues to process commands after a command fails 
    A driver is not required to implement this method. The possible implementations and return values have been modified in the Java 2 SDK, Standard Edition, version 1.3 to accommodate the option of continuing to proccess commands in a batch update after a BatchUpdateException obejct has been thrown. 
    Returns:
    an array of update counts containing one element for each command in the batch. The elements of the array are ordered according to the order in which commands were added to the batch. 
    Throws: 
    SQLException - if a database access error occurs or the driver does not support batch statements. Throws BatchUpdateException (a subclass of SQLException) if one of the commands sent to the database fails to execute properly or attempts to return a result set.
    Since: 
    1.3 
      

  4.   

    to skywoodsky() :
       用a[0]取出是-2?
       如何取得我删除的记录数?
       谢谢
      

  5.   


    用executeBatch()是不是没有办法获得?只能用executeupdate()?
      

  6.   

    int[] a= ps.executeupdate();
    System.out.println(a[0]);
      

  7.   

    skywoodsky() :a[0] 等于 -2
    怎么回事?
      

  8.   

    不是批量sql的话,用executeUpdate吧
      

  9.   

    我单独又做了,每次都是返回-2,我用的oracle数据库
      

  10.   

    oracle就是這樣
    可以看這里http://blog.csdn.net/elimago/archive/2007/07/03/1676516.aspx
      

  11.   

    http://mail-archives.apache.org/mod_mbox/ibatis-dev/200606.mbox/%3c23769093.1149675270958.JavaMail.jira@brutus%3eThe JDBC spec allows vendors to skip certain functionality in executeBatch and this is what
    Oracle does, they return -2 for updates in executeBatch which iBATIS translates to 0. Oracle
    just doesn't return the number of rows being updated for prepared statements.>From the Oracle 9i documentation: "For a prepared statement batch, it is not possible
    to know the number of rows affected in the database by each individual statement in the batch.
    Therefore, all array elements have a value of -2. According to the JDBC 2.0 specification,
    a value of -2 indicates that the operation was successful but the number of rows affected
    is unknown."
      

  12.   

    用oracle数据库返回值就是-2   不管成功与否