Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

java - How to tell number of rows changed from JDBC execution

I'm uncertain as to how to get the number of rows affected from a SQL execution.

I do like this:

boolean isResultSet = statement.execute(arbitrarySQLCommand);

and I can get the number of rows affected from the getUpdateCount() method. That is all fine. The problem I have is when update count is zero. This can either mean:

  1. It was a DML statement but it didn't affect any rows. Zero rows affected is a valid response. I just means that some condition was not met.

  2. It was a non-DML statement (DDL statement most likely) .. which by definition does not change rows so therefore update count is always zero (duh!). Or to put it another way: The concept of update count is meaningless for such statements.

What I would like is to be able to distinguish between situation 1 and 2 above. How?

I'm not interested in statements that produce output so I could also use executeUpdate() but as I see it the return value from that method has the same flaw:

Returns:

either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

Arghhh!
I wish it was:

Returns:

either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) -1 for SQL statements that return nothing



(note: I do not know the contents of arbitrarySQLCommand beforehand)



Final chosen solution

There just doesn't seem to be a true JDBC-like solution to the problem. In my mind the designers of JDBC has made a serious mistake on the getUpdateCount by using the value 0 (zero) to signify a statement that doesn't (by definition) affect rows because zero rows affected is also a perfectly valid value for the outcome of a DML statement.

The only possible solution seems to be to do some kind of pattern matching on the SQL statement to figure out if it is a DML statement (INSERT,UPDATE,DELETE) or another type of SQL statement. Something like this:

  1. Extract first word from arbitrarySQLCommand. A word is terminated by either a whitespace or a EOL line char.
  2. If that word (ignoring case) is either INSERT, UPDATE or DELETE then it is a DML statement and the output from getUpdateCount() is relevant, otherwise the output from getUpdateCount() is irrelevant.

Ugly and error prone. But the only possible solution that came out of this SO question. :-(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best you can do is checking the SQL statement

Set<String> dmlCommands = new HashSet<String>() {
  {
    add("UPDATE"); add("INSERT"); add("DELETE"); //Add more DML commands ....
  }
};
int updateCount = statement.getUpdateCount();
for(String dml : dmlCommands) {
    if(arbitrarySQLCommand.toUpperCase().contains(dml) && updateCount == 0) {
        updateCount = -1;
        break;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...