2017-08-25 105 views
0
try{ 
    Connection conn = getConnection(); 
    String strUpdateQuery = "update payment_table set CREDIT_CARD_NO = ? where PAYMENT_KEY= ?"; 
    PreparedStatement ps =conn.prepareStatement(strUpdateQuery); 
    for(int i=0;i<nodes.getLength();i++){ 
       ps.setString(1,"524364OQNBQQ4291"); 
       ps.setString(2,"20130215123757533280168"); 
       ps.executeUpdate(); 
       conn.commit(); 
    } 
}catch(SQLException e){ 
    e.printStackTrace(); 
} 

即使在檢查主鍵後,即使是單個行也沒有更新是正確的。JDBC - PreparedStatement executeUpdate()返回0

+0

是否缺少的BeginTransaction? –

+6

另外,爲什麼循環? –

+1

你想用你的頭銜傳達什麼?這似乎與你的實際問題無關。 –

回答

2

嘗試批量更新:

void batchUpdate() { 
    String strUpdateQuery = "UPDATE payment_table " + 
          "SET CREDIT_CARD_NO = ? " + 
          "WHERE PAYMENT_KEY= ?"; 
    try (Connection conn = getConnection(); 
     PreparedStatement ps = conn.prepareStatement(strUpdateQuery)) { 
     for (int i = 0; i < nodes.getLength(); i++) { 
      ps.setString(1, "524364OQNBQQ4291"); 
      ps.setString(2, "20130215123757533280168"); 
      ps.addBatch(); 
     } 
     int[] updated = ps.executeBatch(); 
     // can log updated rows from "updated" 
     // conn.commit(); in case autocommit set to false or used conn.setAutoCommit(false) somewhere 
    } 
    catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 
+0

是的,我試過了批量更新也是如此。但也沒有錯誤,沒有運氣。是的,我在我的代碼中使用了conn.setAutoCommit(false)。 –

+0

這個建議與描述的問題並不相關。 –