2016-09-30 162 views
0

我正在使用preparedStatement.executeUpdate()在數據庫中插入一些行,但它不起作用。雖然當我調試它返回值爲1,這意味着該行已被插入,但是當我檢查數據庫它不插入任何行。不知道爲什麼會發生。任何形式的幫助,將不勝感激。以下是我的代碼:executeUpdate方法不能正常工作

Connection connection = null; 
PreparedStatement preparedStatement = null; 

try { 
    connection = getOracleConnection(requestId); 
    preparedStatement = connection.prepareStatement(createAndSaveSummarySQL()); 
    preparedStatement.setString(1, siteId); 
    preparedStatement.setString(PBNConstants.TWO, taskId); 
    preparedStatement.setString(PBNConstants.THREE, notificationType); 
    preparedStatement.setString(PBNConstants.FOUR, clusterId); 


    final long insertStartTime = System.currentTimeMillis(); 

    final int returnValue = preparedStatement.executeUpdate(); 
+0

您使用緩存,在插入後進行回滾?你如何檢查行是否被插入? mybe你有不同的數據庫連接? – Jens

+1

請顯示catch .. finally ...塊 – Jens

+2

請添加您正在獲取的異常。 –

回答

0

請確保以正確方式關閉連接,如下所示。

try { 
      connection = getOracleConnection(requestId);  

    }finally{ 
    if (preparedStatement != null) preparedStatement.close(); 
     if (connection != null) connection.close(); 
    } 
+0

@Jen嗨,謝謝你回覆。我在最後正確關閉連接。還有一件事,我正在調試時檢查插入。所以final int returnValue = preparedStatement.executeUpdate();調試時返回值爲1,但在此之後,當我檢查數據庫時,我看不到任何插入的行。奇怪的是我立即檢查這個語句,它給了我一個錯誤,說「SQL唯一完整性約束違規」,所以相信一行被插入,但我沒有看到它。 的奇怪行爲我看到了,但沒有得到任何結論 –

+0

如果A位(!連接= NULL){ \t \t \t嘗試{ \t \t \t \t的Connection.close(); \t \t \t}趕上(最終的SQLException E){ \t \t \t \t \t \t \t} \t \t} –

+0

@Rohit - 我沒有得到任何的異常。這只是該行沒有被插入,我的應用程序失敗,因爲插入後我根據header_id更新該行並在更新期間失敗,因爲它沒有找到任何要更新的行。 –