Difference Between executeQuery(), executeUpdate() and execute()

Difference Between executeQuery(), executeUpdate() and execute() Methods in Java

executeQuery() Vs executeUpdate() Vs execute()

  • executeQuery() Vs executeUpdate() Vs execute() are the methods of java.sql.Statement interface of JDBC which are used to execute SQL statements.
  • 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.
executeQuery() Vs executeUpdate() Vs execute() difference
executeQuery() Vs executeUpdate() Vs execute() difference

Comparison Chart

executeQuery() executeUpdate() execute()
executeQuery() method used to retrieve some data from database. executeUpdate() method used for update or modify database. execute() use for any SQL statements.
It returns an object of the class

ResultSet executeQuery (String sql) throws SQLException

It returns an integer value.

int executeUpdate(String sql) throws SQLException

It returns a boolean value.

int executeUpdate(String sql) throws SQLException

This method is normally used to execute SELECT queries. This method Is used to execute non SELECT queries.

  • DML as INSERT, DELETE, UPDATE or
  • DDL as CREATE. DROP
This method can be used to execute any type of SQL statement.
Example:

  • ResultSet Ts= stmt.executeQuery(query);
Example:

  • int i= stmt.executeUpdate(query);
Example:

  • Boolean b= stmt.execute(query);

More Difference