

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between execute(), executeQuery() and executeUpdate() methods in JDBC?
Once you have created the statement object you can execute it using one of the execute methods of the Statement interface namely, execute(), executeUpdate() and, executeQuery().
The execute() method: This method is used to execute SQL DDL statements, it returns a boolean value specifying weather the ResultSet object can be retrieved.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Example { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Executing the statement String createTable = "CREATE TABLE Employee( " + "Name VARCHAR(255), " + "Salary INT NOT NULL, " + "Location VARCHAR(255))"; boolean bool = stmt.execute(createTable); System.out.println(bool); } }
Output
Connection established...... false
executeUpdate(): This method is used to execute statements such as insert, update, delete. It returns an integer value representing the number of rows affected.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class ExecuteUpdateExample { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); String insertData = "INSERT INTO Employee(" + "Name, Salary, Location) VALUES " + "('Amit', 30000, 'Hyderabad'), " + "('Kalyan', 40000, 'Vishakhapatnam'), " + "('Renuka', 50000, 'Delhi'), " + "('Archana', 15000, 'Mumbai')"; int i = stmt.executeUpdate(insertData); System.out.println("Rows inserted: "+i); } }
Output
Connection established...... Rows inserted: 4
executeQuery(): This method is used to execute statements that returns tabular data (example select). It returns an object of the class ResultSet.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ExecuteQueryExample { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Retrieving data ResultSet rs = stmt.executeQuery("Select *from Employee"); while(rs.next()) { System.out.print("Name: "+rs.getString("Name")+", "); System.out.print("Salary: "+rs.getInt("Salary")+", "); System.out.print("City: "+rs.getString("Location")); System.out.println(); } } }
Output
Connection established...... Name: Amit, Salary: 30000, City: Hyderabad Name: Kalyan, Salary: 40000, City: Vishakhapatnam Name: Renuka, Salary: 50000, City: Delhi Name: Archana, Salary: 15000, City: Mumbai
- Related Questions & Answers
- What is the difference between the methods setBlob() and setBinaryStream() which is preferable in JDBC?
- What is the difference between ODBC and JDBC
- What is the difference between EXECUTE IMMEDIATE and EXECUTE WITH PREPARE in DB2?
- What is the difference between scipy.cluster.vq.kmeans() and scipy.cluster.vq.kmeans2() methods?
- What is the difference between functions and methods in JavaScript?
- What is the difference between non-static methods and abstract methods in Java?
- What is the difference between the TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE ResultSets in JDBC?
- What is the difference between jQuery Ajax Events and Methods?
- What is the difference between switchClass() and toggleClass() methods in jQuery?
- What is the difference between jQuery.hide() and jQuery.remove() methods in jQuery?
- What is the difference between jQuery.empty() and jQuery.remove() methods in jQuery?
- What is the difference between jQuery.prepend() and jQuery.prependTo() methods in jQuery?
- What is the difference between jQuery.replaceAll() and jQuery.replaceWith() methods in jQuery?
- What is the difference between jQuery.post() and jQuery.get() methods in jQuery?
- What is the difference between jQuery.load() and jQuery.ajax() methods in jQuery?
Advertisements