Open In App

Difference Between Execute(), query() and Update() Methods in Java

Before getting started, let us have prior knowledge of parameters that makes use of the following three queries parameters, which are as follows:

They are discussed in below listed manner as follows:



  1. execute()
  2. execute Query()
  3. execute Update()

Method 1: execute()

Illustration: 






// Java Program to Illustrate usage of execute() Method
   
// Loading the driver using forName() method 
Class.forName(driver);
  
// Registering the driver using Drivermanager.getConnection() method 
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
                  "root", "1234");
  
// Get database connection
stmt = conn.createStatement();
  
// Use Connection to create a Statement object
  
// Execute SQL and return boolean value to 
// indicate whether it contains ResultSet
boolean hasResultSet = stmt.execute(sql);
  
// Condition holds true till there is a single element 
if (hasResultSet) 
{
  
    // If there is a ResultSet result set after execution
    rs = stmt.getResultSet();
    // Get the result set
  
    ResultSetMetaData rsmd = rs.getMetaData();
      
    // ResultSetMetaData is a metadata interface for analyzing result sets
    int  columnCount = rsmd.getColumnCount();
  
    // Getting the output ResultSet object
    // with help of object of ResultSet 
    while (rs.next ()) 
    {
  
        for (int i = 0 ; i < columnCount ; i++ ) 
        {
            System.out.print(rs.getString(i + 1) + "/t");
        }
        System.out.print("/n");
    }
else 
{
  
    System.out.println ("The records affected by this SQL statement are" 
    + stmt.getUpdateCount () + "Article");
}

 
Method 2: execute Query() 

Illustration: 




// Java Program to Illustrate execute Query() Method 
  
// Again first step is to load and register drivers   
Class.forName("com.mysql.jdbc.Driver");
  
Connection  conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
            "root","root");
// Using DriverManager to get database connection
  
Statement  stmt = conn.createStatement();
// Use Connection to create a Statement object
  
// Creating an object of ResultSet class 
ResultSet rs =stmt.executeQuery("select * from teacher");
  
// Execute the query statement and save the result
// Iterating over elements in above object 
while (rs.next()) 
{
  
           // Getting the output the query result
           System.out.println(rs.getInt(1) + "/t" +    rs.getString(2));  
}

Method 3: execute Update()

DML->INSERT , UPDATE and DELETE
DDL-> CREATE, ALTER

Illustration:    

  Class.forName("com.mysql.jdbc.Driver");
    // Load the database driver
 Connection  conn = null;
 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
        "root","1234");
    // Use DriverManager to get database connection
 Statement  stmt = conn.createStatement();
    // Use Connection to create a Statement object


 return stmt.executeUpdate(sql);
    // Execute the DML statement and return the number of records affected

Now let us finally conclude out the differences in return types as spotted in the above illustrations

  1. execute(): The return type is Boolean, indicating whether ResultSet return
  2. executeQuery(): Type method returns a ResultSet, execute returns the results of the query, often used to perform the query
  3. executeUpdate(): The return type is int, that the implementation of a number of rows affected after the sql statement, usually used to execute modification statements.

Article Tags :