Entri Blog
No Result
View All Result
Friday, March 24, 2023
  • State PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Govt Exams
    • Railway
    • SSC
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
  • Courses
    • Data Science Course
      • Data Science Malayalam
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
  • Others
    • GATE
    • MAT
    • KMAT
Try out Spoken English!
Entri Blog
  • State PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Govt Exams
    • Railway
    • SSC
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
  • Courses
    • Data Science Course
      • Data Science Malayalam
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
  • Others
    • GATE
    • MAT
    • KMAT
No Result
View All Result
Entri Blog
Spoken English
banner top article banner top article
Home Articles

Difference Between Execute(), Query() and Update() Methods In Java

by Ayesha Surayya
December 7, 2022
in Articles, Coding, Entri Skilling, Java Programming, Web and Android Development
Difference Between Execute(), Query() and Update() Methods In Java
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • Methods
  •  execute()
  • executeQuery()
  •  executeUpdate()

Java is a multi-platform, object-oriented, and network-centric language that can be used as a platform in itself. It is a fast, secure, reliable programming language for coding everything from mobile apps and enterprise software to big data applications and server-side technologies.

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

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().

Methods

  • execute()
  • executeQuery()
  • executeUpdate()

 execute()

  • Description: The method used for all types of SQL statements, and that is, returns a Boolean value of TRUE or FALSE.
  • Return type: This method return a Boolean value. TRUE indicates that query returned a Result Set object and FALSE indicate returned an int value or returned nothing.
  • Usage: This method is used to execute  Both select and non select queries.
  • Example: All SQL statements.

Illustration:

  • Java
// 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”);

}

Learn to code from expert mentors! Enroll here

executeQuery()

  • Description: Now this method execute statements that returns a result set by fetching some data from the database.
  • Usage: This method is use to execute select query.
  • Return type: This method returns a Result Set object which contains the result returned by query.
  • One of it’s example that is widely common: ‘SELECT’

Illustration:

  • Java
// 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));

}

 executeUpdate()

  • Description: This method is used for execution of DML statement(INSERT, UPDATE and DELETE) which is return int value, count of the affected rows.
  • Usage: This method is use to execute non select query. This method is use to execute select and non select queries.
  • Return type:An integer value which represent number of rows affected by the query. This will be 0 for statement which are returning nothing.
  • Example:

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

Learn to code from expert mentors! Enroll here

Differences in return types 

  • execute(): The return type is Boolean, indicating whether ResultSet return
  • executeQuery(): Type method returns a ResultSet, execute returns the results of the query, often used to perform the query
  • 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.
  • Statement execute(String query)is used to execute any SQL query and it returns TRUE if the result is an ResultSet such as running Select queries. The output is FALSE when there is no ResultSet object such as running Insert or Update queries. We can use getResultSet() to get the ResultSet and getUpdateCount() method to retrieve the update count.
  • Statement executeQuery(String query)is used to execute Select queries and returns the ResultSet. ResultSet returned is never null even if there are no records matching the query. When executing select queries we should use executeQuery method so that if someone tries to execute insert/update statement it will throw java.sql.SQLException with message “executeQuery method can not be used for update”.
  • Statement executeUpdate(String query) is used to execute Insert/Update/Delete (DML) statements or DDL statements that returns nothing. The output is int and equals to the row count for SQL Data Manipulation Language (DML) statements. For DDL statements, the output is 0.

Learn coding in your Language, Download Entri App!

Share66SendShare
Ayesha Surayya

Ayesha Surayya

Related Posts

Top 100 Angular Interview Questions and Answers 2023
Articles

Top 100 Angular Interview Questions and Answers 2023

March 23, 2023
Top 100 SQL query Interview Questions and Answers for 2023
Articles

Top 100 SQL query Interview Questions and Answers for 2023

March 23, 2023
Indian Air Force Agniveer Vayu Selection Process 2023: Details Here
Articles

Indian Air Force Agniveer Vayu Selection Process 2023: Details Here

March 23, 2023
Next Post
Difference Between Java Threads And OS Threads

Difference Between Java Threads And OS Threads

Discussion about this post

Latest Posts

  • Best Career Options After BCA In India
  • IAF Agniveer Vayu Application Form 2023 Out: Download Link, Eligibility
  • UPSC NDA 2022 Previous Year Papers PDF Download
  • Indian Air Force Agniveer Vayu Previous Year Question Paper PDF 2023
  • 20 Simple Rules for Better Pronunciation

Trending Posts

  • states of india and their capitals and languages

    List of 28 States of India and their Capitals and Languages 2023 – PDF Download

    150261 shares
    Share 60102 Tweet 37564
  • List of Government Banks in India 2023: All you need to know

    61860 shares
    Share 24744 Tweet 15465
  • TNPSC Group 2 Posts and Salary Details 2022

    39693 shares
    Share 15877 Tweet 9923
  • KSDA Recruitment 2023 Apply Online for 9264 FDA SDA Posts – Qualification

    2076 shares
    Share 830 Tweet 519
  • New Map of India with States and Capitals 2023

    28793 shares
    Share 11517 Tweet 7198

Courses

  • Data Science Course
  • Full Stack Developer Course
  • Data Science Course in Malayalam
  • Full Stack Developer Course in Malayalam
  • Full Stack Developer Course in Hindi
  • Full Stack Developer Course in Tamil
  • Full Stack Developer Course in Telugu
  • Full Stack Developer Course in Kannada

Company

  • Become a teacher
  • Login to Entri Web

Quick Links

  • Articles
  • Videos
  • Entri Daily Quiz Practice
  • Current Affairs & GK
  • News Capsule – eBook
  • Preparation Tips
  • Kerala PSC Gold
  • Entri Skilling

Popular Exam

  • IBPS Exam
  • SBI Exam
  • Railway RRB Exam
  • Kerala PSC
  • Tamil Nadu PSC
  • Telangana PSC
  • Andhra Pradesh PSC
  • MPPSC
  • UPPSC
  • Karnataka PSC
  • Staff Selection Commission Exam

© 2021 Entri.app - Privacy Policy | Terms of Service

No Result
View All Result
  • State PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Govt Exams
    • Railway
    • SSC
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
  • Courses
    • Data Science Course
      • Data Science Malayalam
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
  • Others
    • GATE
    • MAT
    • KMAT

© 2021 Entri.app - Privacy Policy | Terms of Service