Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 483
neeraj narang Avatar asked Jan 09 '12 11:01

neeraj narang


People also ask

What is the difference between execute and executeUpdate?

executeQuery() command used for getting the data from database whereas executeUpdate() command used for insert,update,delete or execute() command used forany kind of operations.

Why executeUpdate () is used?

int executeUpdate(String SQL): Returns the number of rows affected by the execution of the SQL statement. Use this method to execute SQL statements, for which you expect to get a number of rows affected – for example, an INSERT, UPDATE, or DELETE statement.

What is difference between execute executeQuery and executeUpdate in Java?

executeUpdate() : This method is used for execution of DML statement(INSERT, UPDATE and DELETE) which is return int value, count of the affected rows. executeQuery() : This method is used to retrieve data from database using SELECT query.

What is the use of executeUpdate () method?

The executeUpdate() method returns the number of rows affected by the SQL statement (an INSERT typically affects one row, but an UPDATE or DELETE statement can affect more).


1 Answers

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.

like image 104
Sjoerd Avatar answered Oct 25 '22 02:10

Sjoerd