What to use: executeUpdate() or execute()?

11,728

Solution 1

The return value differs. ExecuteUpdate() returns the number of rows updated, which can be useful when running an update statement. In your case it is not needed, since you know how many records you are inserting. You can use either one.

Solution 2

In principle only the return value is different. However, I found that using jConnect 3 to access Sybase ASE 15.7, the execute() function does not block until triggers have run and closing the PreparedStatement straight away will ROLL BACK the update. (Inserting a 1s sleep makes it work for one query I tried.) By contrast, executeUpdate() does not suffer from this problem; it appears to do the right thing, and does not require an arbitrary sleep before closing the PreparedStatement.

Share:
11,728

Related videos on Youtube

neeraj narang
Author by

neeraj narang

Updated on June 04, 2022

Comments

  • neeraj narang
    neeraj narang almost 2 years

    Please tell me which out of the two methods, executeUpdate and execute is the best one for an insert query like insert into users(name, addr, city, sex, dob) values(?,?,?,?,?);Both the statements would execute the query but which one should be ideally used for an insert query?

  • neeraj narang
    neeraj narang over 12 years
    So there are no advantages as such in using either one of them right? In this case it doesn't matter which method is used right?
  • Anthony Grist
    Anthony Grist over 12 years
    From a readability standpoint, I think executeUpdate() makes it clearer that there will be changes to the data as a result of the statement being executed.