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

How to add conditional expressions to aggregate functions in MySql

MySQL filtering timing of where conditions and ha...

Differences in the hr separator between browsers

When making a web page, you sometimes use a dividi...

CSS pseudo-class: empty makes me shine (example code)

Anyone who has read my articles recently knows th...

Simple use of Vue vee-validate plug-in

Table of contents 1. Installation 2. Import 3. De...

Example code for mixing float and margin in CSS

In my recent studies, I found some layout exercis...

How to use vue-cli to create a project and package it with webpack

1. Prepare the environment (download nodejs and s...

Detailed explanation of MySQL slow log query

Slow log query function The main function of slow...

How to deploy DoNetCore to Alibaba Cloud with Nginx

Basic environment configuration Please purchase t...

MySQL study notes on handling duplicate data

MySQL handles duplicate data Some MySQL tables ma...

Analysis of Facebook's Information Architecture

<br />Original: http://uicom.net/blog/?p=762...

Native js to achieve accordion effect

In actual web page development, accordions also a...

How to use union all in MySQL to get the union sort

Sometimes in a project, due to some irreversible ...

Introduction to Docker containers

Docker Overview Docker is an open source software...

Practical method of deleting associated tables in MySQL

In the MySQL database, after tables are associate...