What are different form of execute statement? Explain each form with suitable syntax and example.
There are 4 important methods to execute the query in execute Statement interface. These are explained below:
ResultSet executeQuery(String sql)
int executeUpdate(String sql)
boolean execute(String sql)
int []executeBatch()
a) ResultSet executeQuery(String sql)
The executeQuery() method in Statement interface is used to execute the SQL query and retrieve the values from DB. It returns the ResultSet object. Normally, we will use this method for the SELECT query.
b) executeUpdate(String sql)
The executeUpdate() method is used to execute value specified queries like INSERT, UPDATE, DELETE (DML statements), or DDL statements that return nothing. Mostly, we will use this method for inserting and updating.
c) execute(String sql)
The execute() method is used to execute the SQL query. It returns true if it executes the SELECT query. And, it returns false if it executes INSERT or UPDATE query.
d) executeBatch()
This method is used to execute a batch of SQL queries to the Database and if all the queries get executed successfully, it returns an array of update counts. We will use this method to insert/update the bulk of records.
Comments
Post a Comment