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

Linux firewall status check method example

How to check the status of Linux firewall 1. Basi...

Detailed discussion of the differences between loops in JavaScript

Table of contents Preface Enumerable properties I...

Tutorial on installing mysql under centos7

Recently, I plan to deploy a cloud disk on my hom...

Detailed tutorial on deploying apollo with docker

1. Introduction I won’t go into details about apo...

10 Deadly Semantic Mistakes in Web Typography

<br />This is from the content of Web front-...

Native JS music player

This article example shares the specific code of ...

Simple use of Vue bus

Simple use of Vue bus Scenario description: Compo...

Gogs+Jenkins+Docker automated deployment of .NetCore steps

Table of contents Environmental Description Docke...

Ubuntu starts the SSH service remote login operation

ssh-secure shell, provides secure remote login. W...

How to migrate local mysql to server database

We can use the scp command of Linux (scp cannot b...

Solve the problem of Docker starting Elasticsearch7.x and reporting an error

Using the Docker run command docker run -d -p 920...

Add ico mirror code to html (favicon.ico is placed in the root directory)

Code: Copy code The code is as follows: <!DOCTY...

Analyze the problem of pulling down the Oracle 11g image configuration in Docker

1. Pull the image docker pull registry.cn-hangzho...