gpt4 book ai didi

java - 为什么execute()在空表上返回true?

转载 作者:行者123 更新时间:2023-12-02 07:21:35 25 4
gpt4 key购买 nike

通过以下代码片段,我尝试运行一个查询来更新数据或将新数据插入名为 JustPinged 的表中。该表包含名为 NodesThatJustPingedLastPingedAt 的列。如果 NodesThatJustPinged 中已存在节点,则更新 LastPingedAt 中的时间(以毫秒为单位)。否则,将插入新的节点信息。

问题是,以下代码片段无法将数据插入数据库表中。原因是这样的说法:

boolean duplicateExists = searchToEliminateDuplicates.execute();

首先返回true。 (最初表是空的)为什么这个语句返回true?根据documentation如果第一个结果是 ResultSet 对象,则返回 true;如果第一个结果是更新计数或没有结果,则为 false。 因此这里的 boolean 值应包含 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");
}

那么我应该如何编辑代码,以便更新重复值并插入新值?

最佳答案

您的 searchQuery 将返回 ResultSet。因此执行方法返回“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。 execute() 用于您事先不知道查询是 INSERT 还是 UPDATE 的情况。

PPPS 使用完结果集和报表后立即关闭它们。

PPPPS 更好的方法是在 SQL 语句中使用计数聚合函数,即

从 JustPinged 中选择 count(NodesThatJustPinged),其中 NodesThatJustPinged = '"+ nodeInfo + "'";

现在您可以检查 count 是否为 0 或大于 1,并相应地分支您的代码。

关于java - 为什么execute()在空表上返回true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14151772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com