2013-01-04 31 views
0

通過以下代碼片段,我試圖運行一個查詢來更新數據或向名爲JustPinged的表中插入新數據。該表包含名爲NodesThatJustPingedLastPingedAt的列。如果在NodesThatJustPinged中已經有節點,則LastPingedAt中的時間以毫秒爲單位進行更新。否則會插入一個新的node信息。爲什麼execute()在空表上返回true?

的問題是,下面的代碼片段是無法將數據插入到數據庫中的表。原因是該語句:

boolean duplicateExists = searchToEliminateDuplicates.execute(); 

回報true開始。 (最初這個表是空的)爲什麼這個語句返回true?根據documentation,如果第一個結果是ResultSet對象,則返回;如果第一個結果是更新計數或沒有結果,則爲false。所以這裏的布爾應該包含一個錯誤的值。但它包含一個true值,因此if聲明總是有效。 (在if部分,更新查詢時,沒有什麼更新的作品!)

String searchQuery = "select NodesThatJustPinged from JustPinged where NodesThatJustPinged = '" + nodeInfo + "'"; 
PreparedStatement searchToEliminateDuplicates = connection.prepareStatement(searchQuery); 
boolean duplicateExists = searchToEliminateDuplicates.execute(); 

if(duplicateExists) { 
    // update the LastPingedAt column in the JustPinged table 
    String updateQuery = "update JustPinged set LastPingedAt='" + pingedAt + "' where NodesThatJustPinged = '" + nodeInfo + "'"; 
    PreparedStatement updateStatement = connection.prepareStatement(updateQuery); 
    updateStatement.executeUpdate();System.out.println("If statement"); 
} else { 
    // make a new entry into the database 
    String newInsertionQuery = "insert into JustPinged values('" + nodeInfo + "','" + pingedAt + "')"; 
    PreparedStatement insertionStatement = connection.prepareStatement(newInsertionQuery); 
    insertionStatement.executeUpdate();System.out.println("else statement");    
} 

所以我應該怎麼修改代碼,使重複的​​值將被更新和插入新的價值?

回答

1

您的searchQuery將返回ResultSet。因此execute方法返回'true'。嘗試使用executeQuery來代替。

所以,你的代碼將變成:

String searchQuery = "select NodesThatJustPinged from JustPinged where NodesThatJustPinged = '" + nodeInfo + "'"; 
    Statement searchToEliminateDuplicates = connection.createStatement(); 
    ResultSet duplicateExists = searchToEliminateDuplicates.executeQuery(searchQuery); 

    if(duplicateExists.next()) { 
     // update the LastPingedAt column in the JustPinged table 
     String updateQuery = "update JustPinged set LastPingedAt='" + pingedAt + "' where NodesThatJustPinged = '" + nodeInfo + "'"; 
     PreparedStatement updateStatement = connection.prepareStatement(updateQuery); 
     updateStatement.executeUpdate();System.out.println("If statement"); 
    } else { 
     // make a new entry into the database 
     String newInsertionQuery = "insert into JustPinged values('" + nodeInfo + "','" + pingedAt + "')"; 
     PreparedStatement insertionStatement = connection.prepareStatement(newInsertionQuery); 
     insertionStatement.executeUpdate();System.out.println("else statement");    
     } 

附:如果您使用的PreparedStatement,然後在查詢中使用的參數,並調用ps.setString等

PPS。不要使用execute()方法。使用executeQuery或executeUpdate。在您不知道您的查詢是INSERT還是UPDATE的情況下使用execute()。

公私夥伴關係,很快就閉上你的結果集和語句,你與他們所做的。

PPPPS更更好的方法是使用SQL語句計數聚合函數即

從JustPinged其中NodesThatJustPinged =「」 +的nodeinfo + 「'」

SELECT COUNT(NodesThatJustPinged);

現在你可以檢查計數是否大於1或0,並相應的代碼分支

1

一個SELECT語句返回零行仍然會返回一個ResultSet - 。只是一個下一個(調用時立即返回false),您需要檢查返回中的行數ned ResultSet。

相關問題