what is the harm of using executeQuery instead of executeUpdate for deleting rows
In my code I am using
String query= "delete all from myTable";
stmt.executeQuery(query);
Ideally for DML executeUpdate should be used instead. However, executeQuery too works well. So, was curious to know what could be the ha开发者_JS百科rm of using executeQuery instead of executeUpdate?
First, you don't get the number of updated row.
Second, depends on the JDBC driver, your query may be failed.
It does not tell you how many rows were affected.
It creates a ResultSet (or maybe not?) of questionable value.
And are you sure that this works well with all databases? The Javadoc says that the driver can throw an SQLException if "the given SQL statement produces anything other than a single ResultSet object", for which DELETE probably qualifies.
If you really do not know if you are about to run a query or DML, you could use execute(), which works for both, and then lets you call getUpdateCount
or getResultSet
and even getMoreResults
.
精彩评论