2011-03-07 33 views
3

我想执行下列确切SQL查询:如何获取包含在批处理例外

(从处理数据库的读取行转移到PRESENTATION DB一排)

PreparedStatement stmt; 
    Statement st; 
    Connection conn = ConnectionManager.getInstance().getConnection(ConnectionManager.PRESENTATION_DB); 
    try { 
     conn.setAutoCommit(false); 
     stmt = conn.prepareStatement(
       "insert into customer (name,category,age) values (?,?,?)"); 
        stmt.clearParameters(); 
        stmt.setString(1,"name2"); 
        stmt.setString(2,"cat2"); 
        stmt.setInt(3,25); 
        stmt.addBatch(); 
        stmt.clearParameters(); 
        stmt.setString(1,"name3"); 
        stmt.setString(2,"cat3"); 
        stmt.setInt(3,25); 
        stmt.addBatch(); 
        stmt.clearParameters(); 
        stmt.setString(1,"name4"); 
        stmt.setString(2,"cat2"); 
        stmt.setInt(3,25); 
        stmt.addBatch(); 
        stmt.clearParameters(); 
        stmt.setString(1,"name4"); 
        stmt.setString(2,"cat2"); 
        //stmt.setInt(3,25); 

        //this is parameter with false input 
        stmt.setString(3,"null"); 

        stmt.addBatch(); 
        stmt.clearParameters(); 
        stmt.setString(1,"name5"); 
        stmt.setString(2,"cat5"); 
        stmt.setInt(3,25); 
        stmt.addBatch(); 
        stmt.clearParameters(); 

     int [] updateCounts = stmt.executeBatch(); 
     conn.commit(); 
     conn.setAutoCommit(true); 

        stmt.close(); 
     conn.close(); 
    } 
    catch(BatchUpdateException b) { 
        System.err.println("-----BatchUpdateException-----"); 
        System.err.println("SQLState: " + b.getSQLState()); 
        System.err.println("Message: " + b.getMessage()); 
        System.err.println("Vendor: " + b.getErrorCode()); 
        System.err.print("Update counts: "); 
        int [] updateCounts = b.getUpdateCounts(); 
        for (int i = 0; i < updateCounts.length; i++) { 
          System.err.print(updateCounts[i] + " "); 
        } 
        System.err.println(""); 
      } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 

如何识别导致异常能够在没有这个特定行的情况下重新执行批处理的行? (通过更新行的字段hasError)。

目前我不是通过批量保存对象,因此如果它导致错误并跳过该行并继续处理(即保存/传输和删除)另一行,则可以标记该行。

谢谢!

更新:

好的,我用下面的代码来跟踪行(如果有的话行导致错误):

 public static void processUpdateCounts(int[] updateCounts) { 
      for (int i=0; i<updateCounts.length; i++) { 
       if (updateCounts[i] >= 0) { 
        // Successfully executed; the number represents number of affected rows 
        logger.info("Successfully executed: number of affected rows: "+updateCounts[i]); 
       } else if (updateCounts[i] == Statement.SUCCESS_NO_INFO) { 
        // Successfully executed; number of affected rows not available 
        logger.info("Successfully executed: number of affected rows not available: "+updateCounts[i]); 
       } else if (updateCounts[i] == Statement.EXECUTE_FAILED) { 
        logger.info("Failed to execute: "+updateCounts[i]); 
        // Failed to execute 
       } 
      } 
     } 

而且增加了行:

 processUpdateCounts(updateCounts); 

所以我可以看到有或没有任何错误的变化,但它看起来像BatchUpdateException不起作用?从的BatchUpdateException的的Javadoc

回答

0

行情:

元件中的 更新计数阵列的顺序对应于其中 命令加入到 批次的顺序。

在 无法正确执行的批处理更新的命令和一个 的BatchUpdateException被抛出后, 驱动器可以或者可以不继续 过程在 批处理中的剩余命令。如果驾驶员在发生故障后继续 处理,阵列 通过该方法 让BatchUpdateException.getUpdateCounts 批次中的返回将有一个元件,用于每个命令 而不是只元件 该误差之前执行 成功的命令。在驱动程序继续执行 处理命令的 情况下,对于任何失败的命令,数组元素 是 Statement.EXECUTE_FAILED。

+0

嗨,感谢您的即时回复,我已更新我的问题,并希望能为此输入任何信息,谢谢! – eunique0216 2011-03-08 05:50:24

0

正如已经指出的那样,您只能访问在批处理期间失败的SQL语句的索引。这意味着,为了创建相同的语句执行失败,你应该存储在可转位方式的参数,如:

List<...>paramList = new ArrayList<...>(); 
for (... params in paramList) { 
    stmt.clearParameters(); 
    stmt.setString(...something involving params...); 
    // etc. 
} 

这样,getUpdateCountsEXECUTE_FAILED阵列的指数可以用来识别哪些参数导致失败。

0

创建一个帮助执行命令的数据库层,如果sql命令使用while循环重试几次,如果几次后仍然失败,则记录它。

0

我正在使用mysql-connector-java-3.1.14和MySQL。本主题顶部的更新(使用BatchUpdateException和getUpdateCounts)正在正常工作。现在我可以在批量插入/更新中找到有问题的行。

相关问题