Vik
Vik

Reputation: 9289

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 harm of using executeQuery instead of executeUpdate?

Upvotes: -1

Views: 1040

Answers (2)

Thilo
Thilo

Reputation: 262534

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.

Upvotes: 1

nanda
nanda

Reputation: 24788

First, you don't get the number of updated row.

Second, depends on the JDBC driver, your query may be failed.

Upvotes: 2

Related Questions