The difference between MySQL execute, executeUpdate and executeQuery

The difference between MySQL execute, executeUpdate and executeQuery

The differences among execute, executeUpdate, and executeQuery (and their return values)

1. boolean execute(String sql)

Allows execution of query statements, update statements, and DDL statements.

When the return value is true, it means that a query statement is executed, and the result can be obtained through the getResultSet method; when the return value is false, an update statement or DDL statement is executed, and the getUpdateCount method obtains the number of updated records.

example:

public static void main(String[] args) { 
 
 Connection conn = null; 
 Statement stm = null; 
 ResultSet rs = null; 
 try { 
  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
  conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test;user=sa;password=sasa"); 
  stm = conn.createStatement(); 
  boolean ret = stm.execute("select * from stuinfo"); 
  if(ret){ 
  rs = stm.getResultSet(); 
  while(rs.next()){ 
   System.out.println("Name: "+rs.getString("stuName")+"\tAge: "+rs.getString("stuScore")); 
  } 
  } 
  ret = stm.execute("update stuinfo set stuScore=62 where stuname='张三'"); 
  int count = stm.getUpdateCount(); 
  if(!ret){ 
  System.out.println(count+"The data was modified successfully!"); 
  } 
 } catch (ClassNotFoundException e) { 
  e.printStackTrace(); 
 } catch (SQLException e) { 
  e.printStackTrace(); 
 }  
 } 

2. int executeUpdate(String sql)

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement, or an SQL statement that returns nothing (such as an SQL DDL statement).

The return value is the number of records updated.

3. ResultSet executeQuery(String sql)

Executes the given SQL statement, which returns a single ResultSet object.

execute is a combination of executeUpdate and executeQuery

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Understand the difference between submit and execute through code examples
  • PHP PDOStatement::execute Explained
  • Solution to the issue of JDBC Oracle executing executeUpdate and getting stuck
  • Tutorial on using prepare, execute and deallocate statements in MySQL
  • Failed to execute goal org...Solution
  • ThreadPoolExecutor thread pool principle and its execute method (detailed explanation)
  • Use and precautions of Python executemany
  • What is the difference between execute and submit?

<<:  How to use Dockerfile to create a mirror of the Java runtime environment

>>:  How to reference external CSS files and iconfont in WeChat applet wxss

Recommend

CSS3 uses animation attributes to achieve cool effects (recommended)

animation-name animation name, can have multiple ...

Detailed explanation of the principle of creating tomcat in Eclipse

When creating a tomcat server on a local eclipse,...

Detailed tutorial for installing mysql5.7.21 under Windows system

MySQL Installer provides an easy-to-use, wizard-b...

WeChat Mini Program to Implement Electronic Signature

This article shares the specific code for impleme...

Introduction to MySQL <> and <=> operators

<> Operator Function: Indicates not equal t...

Thirty HTML coding guidelines for beginners

1. Always close HTML tags In the source code of p...

HTML tutorial, understanding the optgroup element

Select the category selection. After testing, IE ...

Detailed explanation of the use of CSS3 rgb and rgba (transparent color)

I believe everyone is very sensitive to colors. C...

Some common advanced SQL statements in MySQL

MySQL Advanced SQL Statements use kgc; create tab...

CSS3 property line-clamp controls the use of text lines

Description: Limit the number of lines of text di...

Vue integrates PDF.js to implement PDF preview and add watermark steps

Table of contents Achieve results Available plugi...

Automated front-end deployment based on Docker, Nginx and Jenkins

Table of contents Preliminary preparation Deploym...

Solution to the docker command exception "permission denied"

In Linux system, newly install docker and enter t...