简体   繁体   中英

JDBC getUpdateCount is returning 0, but 1 row is updated, in SQL Server

Anyone ever seen this? I'm using MS SQL Server 2008, and I've tried it with two different JDBC drivers (jtds, and Microsoft's). I run a simple statement to update a row, and it does update it, but getUpdateCount returns 0. If I try it for different tables, it returns 1 as expected. It's something about this one table.

PreparedStatement ps = 
  conn.prepareStatement("select count(*) from foo_users where user_id = 1")
ResultSet rs = ps.executeQuery();
rs.next()
println(" count(*) is " + rs.getInt(1));    // Prints 1

ps = conn.prepareStatement("update foo_users set is_admin = 1 where user_id = 1")
ps.execute()
int count = ps.getUpdateCount()
println(" update count is " + count)        // Prints 0.  WTF.

What is causing this?

Update in response to comment: Yes, executeUpdate works. But I ask this question because I'm using a query library called jOOQ which is returning incorrect results because it's calling execute and getUpdateCount . I left this out of my question originally, because I don't think it is the library's fault.

You need "executeUpdate" for your update statement.

executeUpdate returns the rowcount. "execute" just returns false if there is no recordset returned, which there wouldn't be for an UPDATE.

So I am facing a very weird issue right now regarding the same thing. Any leads will be appreciated - So this is the code that was written previously by some other developer and I am relatively new to Java & JDBC

boolean getResultSet = cstmt.execute();
int updateCount = -1;
while (true) {
    if (getResultSet) {
        ResultSet r = cstmt.getResultSet();
        while (r.next()) {}
        r.close();
    } else {
    updateCount = cstmt.getUpdateCount();
    if (updateCount != -1) {
        // process update count if expected/wanted
    }
  }
    if ((!getResultSet) && (updateCount == -1)) {
        break; // done with loop
    }
    getResultSet = cstmt.getMoreResults();
}

I am not able to grasp what exactly is happening here.

I have 2 duplicate DBs, both hosted on different servers.

The same code is working on one DB and not on another DB.

So here, the execute() is returning false on both the servers but getUpdateCount() is return 0 for one server and 1/-1 for the other.

Unable to understand what exactly getUpdateCount() and execute() do.

I had a similar problem, it is bound to some sort of optimization of the driver that does not do the update if the clumn has already that value

Try to think at your update query like:

update foo_users set is_admin = 1 where user_id = 1 and is_admin <> 1 

If is_admin colum is already equals to 1, then no updates will be executed and affected records will be zero because getUpdateCount() returns the number of records updated not the matched ones.

You can play with executeUpdate() or useAffectedRows=true to get your desired result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM