ExecuteQuery(…), ExecuteUpdate(…) and Execute

What are the differences between executeQuery(…), executeUpdate(…) and execute(…) methods?

Where executeQuery() can be used to execute selection group sql queries to fetch the data from database.

When we use selection group sql query with executeQuery() then JVM will send that sql query to the database engine, database engine will execute it, by this database engine(DBE) will fetch the data from database and send back to the java application.

Java is a purely object oriented technology. That’s why the jdbc application will maintain the fetched data from database, in the form of an object at heap memory, called as ResultSet object.

            public  ResultSet executeQuery(String sqlquery)

Where executeUpdate() can be used to execute updation group sql query to update the database. When we provide updation group sql query as a parameter to executeUpdate(), then JVM will send that sql query to DBE, here DBE will execute it and perform updations on the database, by this DBE will identify the number of records got updated value called as “records updated count” and return back to the java application.

            public int executeUpdate(String sqlquery)

Where execute() can be used to execute either selection group sql queries or updation group queries.

When we use selection group sql query with the execute() then we will get ResultSet object at heap memory with the fetched data. But execute() will return “true” as a Boolean value.

When we use updation group sql query with execute() then we will get “ records updated count value” at jdbc application. But execute() will return “false” as a Boolean value.

            public  boolean execute(String sqlquery)
 

How to  execute SQL Queries from a java application?

To execute the sql queries we will use the following methods from Statement object.

  • st.executeQuery(…)
  • st.executeUpdate(…)
  • st.execute(…)
What is the requirement to use Statement object? 

After establishing a connection between java application and the database we need to write the sql queries and we need to execute them.

To execute the sql queries we will use some pre-defined library, which was defined in the form of Statement object,  PreparedStattement object and CallableStatement object.

As per the application requirements we need to create either Statement object or CallableStatement object and PreparedStatement object.

To create Statement object dwe will use the following method from connection object.

public  Statement createStatement()
Eg:     Statement st = con.createStatement();

See also
Important Servlet Questions

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.