2013-01-04 33 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。

相关问题