tags:

views:

24

answers:

2

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?

+1  A: 

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

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

nanda
what if i dont need to know that?
Vik
You can use the method if you insist to do that and there is no bad effect you can observe. But doing that means you also ready to modify the application if the JDBC driver get updated (and bahave differently).
nanda
+1  A: 

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.

Thilo
i dont need all the databases to work with. just oracle db
Vik