Statement.executeUpdate()返回-1时的含义是什么? [英] What does it mean when Statement.executeUpdate() returns -1?

查看:931
本文介绍了Statement.executeUpdate()返回-1时的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在管理工作室和 executeUpdate 中运行的查询使得相同的 executeUpdate 返回 -1 ,在我们可以找到的任何文档中都未定义。它应该只返回rowcount或 0 。这是什么意思?如果重要,驱动程序是JDBC-ODBC桥。

A query that works in management studio and in the executeUpdatemakes that same executeUpdate return -1, which is undefined in any documentation we can find. Its supposed to return only the rowcount or 0. What does this mean? The driver is the JDBC-ODBC bridge if that matters.

示例:

String query = "IF NOT EXISTS (SELECT * FROM animals WHERE animal_name ='" + a +"') INSERT INTO " + table + " (animal_name, animal_desc, species_id) VALUES ('" + a + "', '" + b + "', " + c + ")";
int result = statement.executeUpdate(query);
System.out.println(result);

查询有效,因为行被添加到数据库中,它返回-1只是奇怪文档说它只会返回0或rowcount(因为我已经更正)。

The query works, as the row is added to the database, it's just strange that it returns -1 where the documentation says it will only return 0 or the rowcount (as I've been corrected).

更新:

在Management Studio中运行此结果,并且命令已成功完成。

Running this in Management Studio results with "Command completed successfully."

IF NOT EXISTS (SELECT * FROM animals WHERE animal_name = 'a') 
INSERT INTO animals(animal_name, animal_desc, species_id) VALUES ('a', 'a', 1)

这应该意味着该方法应该返回0,因为它不返回任何内容,对吗?

That should mean the method should return 0 because it doesn't return anything, correct?

推荐答案

4年后, Microsoft在Github上开源了他们的JDBC驱动程序。我今天收到了关于这个问题的通知,然后去了看看,我相信我找到了罪魁祸首这里 mssql-jdbc / src / main / java / com / microsoft / sqlserver /jdbc/SQLServerStatement.java:1713

So 4 years later, Microsoft has open sourced their JDBC driver on Github. I got a notification about this question today, and went and had a look, and I believe I have found the culprit here, mssql-jdbc/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java:1713.

基本上,驱动程序试图了解SQL Server发回的内容,如果它不是一个明确的结果组。根据评论,它是这样的:

Basically, the driver tries to understand what SQL Server sends back if it is not a definite result set. According to the comments, it goes like this:



  1. 首先检查错误。 (1669年)

  1. Check for errors first. (ln 1669)

不是错误。它是结果集吗? (1680年)

Not an error. Is it a result set? (ln 1680)

不是错误或结果集。也许是T-SQL语句的结果?
即,以下之一:

Not an error or a result set. Maybe a result from a T-SQL statement? That is, one of the following:


  • 受影响的行数的正数(来自INSERT,UPDATE或DELETE),

  • 零表示没有行受影响,或者语句是DDL,或

  • a -1表示语句成功,但没有可用的更新计数信息(转换为批量更新计数数组中的Statement.SUCCESS_NO_INFO)。 (ln 1706)

  • a positive count of the number of rows affected (from INSERT, UPDATE, or DELETE),
  • a zero indicating no rows affected, or the statement was DDL, or
  • a -1 indicating the statement succeeded, but there is no update count information available (translates to Statement.SUCCESS_NO_INFO in batch update count arrays). (ln 1706)

以上都不是。最后一次机会......进入上面的解析器,我们知道更多结果最初是真的。如果我们发出moreResults false,我们点击DONE标记(DONE(FINAL)或DONE(批量生成)),表示批处理总体成功,但没有关于单个语句更新计数的信息。这与上面的最后一种情况类似,只是没有更新计数。那就是:我们有一个成功的结果(返回true),但我们没有关于它的其他信息(updateCount = -1)。 (ln 1693)

None of the above. Last chance here... Going into the parser above, we know moreResults was initially true. If we come out with moreResults false, the we hit a DONE token (either DONE (FINAL) or DONE (RPC in batch)) that indicates that the batch succeeded overall, but that there is no information on individual statements' update counts. This is similar to the last case above, except that there is no update count. That is: we have a successful result (return true), but we have no other information about it (updateCount = -1). (ln 1693)

只有到达这里的方式(moreResults仍然是真的,但没有任何明显的结果)是TDSParser实际上并没有解析
任何。也就是说,我们在响应中处于EOF状态。在那种情况下,确实没有更多的结果。我们完成了。 (ln 1717)

Only way to get here (moreResults is still true, but no apparent results of any kind) is if the TDSParser didn't actually parse anything. That is, we are at EOF in the response. In that case, there truly are no more results. We're done. (ln 1717)


(强调我的)

所以你们最终都是对的。 SQL根本无法判断受影响的行数,默认为 -1 。 :)

So you guys were right in the end. SQL simply can't tell how many rows are affected, and defaults to -1. :)

这篇关于Statement.executeUpdate()返回-1时的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆