title: How to tell number of rows changed from JDBC execution
body: <p>I'm uncertain as to how to get the number of rows affected from a SQL execution.</p>
  
  <p>I do like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>boolean isResultSet = statement.execute(arbitrarySQLCommand);
  </code></pre>
  
  <p>and I can get the number of rows affected from the <code>getUpdateCount()</code> method. That is all fine. The problem I have is when update count is zero. This can either mean:</p>
  
  <ol>
  <li><p>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.</p></li>
  <li><p>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 <em>update count</em> is meaningless for such statements.</p></li>
  </ol>
  
  <p>What I would like is to be able to <strong>distinguish between situation 1 and 2 above</strong>. How?</p>
  
  <p>I'm not interested in statements that produce output so I could also use <a href="http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate%28java.lang.String%29" rel="noreferrer">executeUpdate()</a> but as I see it the return value from that method has the same flaw:</p>
  
  <blockquote>
    <p>Returns:</p>
    
    <p>either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing</p>
  </blockquote>
  
  <p>Arghhh!<br>
  I wish it was:</p>
  
  <blockquote>
    <p>Returns:</p>
    
    <p>either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) -1 for SQL statements that return nothing</p>
  </blockquote>
  
  <p><br>
  <br>
  (note: I do not know the contents of <code>arbitrarySQLCommand</code> beforehand)</p>
  
  <p><br>
  <br></p>
  
  <h2>Final chosen solution</h2>
  
  <p>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 <code>getUpdateCount</code> 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.</p>
  
  <p>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:</p>
  
  <ol>
  <li>Extract first word from <code>arbitrarySQLCommand</code>. A word is terminated
  by either a whitespace or a EOL line char. </li>
  <li>If that word (ignoring
  case) is either INSERT, UPDATE or DELETE then it is a DML statement
  and the output from <code>getUpdateCount()</code> is relevant, otherwise the
  output from <code>getUpdateCount()</code> is irrelevant.</li>
  </ol>
  
  <p>Ugly and error prone. But the only possible solution that came out of this SO question. :-(</p>