1. 程式人生 > >關於Spring的JdbcTemplate批量更新batchUpdate()方法的返回值為-2的異常

關於Spring的JdbcTemplate批量更新batchUpdate()方法的返回值為-2的異常

近日在使用JdbcTemplate做大量的資料庫資料同步的工作,基本就是“從A庫查詢資料--生成CSV--解析成SQL插入B庫”這樣一個過程。因為使用的是JdbcTemplate,所以在將資料匯入B庫時採用的是“先批量刪,再批量插入”的策略。如果B庫有相同主鍵的記錄,則會先刪掉,然後再將資料批量插入,這樣就避免了主鍵衝突異常。這樣更新資料量=刪除記錄數,新增資料量=總資料量-更新資料量。 

但是在使用batchUpdate()函式時遇到了執行結果為-2的問題,先查查API: 

Java程式碼  收藏程式碼
  1. batchUpdate  
  2. public int[] batchUpdate(String[] sql)  
  3.                   throws DataAccessException  
  4. Description copied from interface: JdbcOperations  
  5. Issue multiple SQL updates on a single JDBC Statement using batching.  
  6. Will fall back to separate updates on a single Statement if the JDBC driver does not support batch updates.  
  7. Specified by:  
  8. batchUpdate in interface
     JdbcOperations  
  9. Parameters:  
  10. sql - defining an array of SQL statements that will be executed.  
  11. Returns:  
  12. [b]an array of the number of rows affected by each statement[/b]  
  13. Throws:  
  14. DataAccessException - if there is any problem executing the batch  


返回值為一個整型的陣列,陣列的每個元素值對應每條SQL影響的資料庫的記錄數。 
這樣無論如何負值也無法理解的。網上查詢有地方說是驅動的問題,然後動手用JDBC的PreparedStatement測試了一把,同樣的結果。那麼說,出現這種結果的根本原因是JDBC驅動。 


從Oracle的官網下載了Oracle11gR2的最新驅動ojdbc6.jar,目前問題仍然存在。。。