Maison > base de données > tutoriel mysql > Quelle est la différence entre les méthodesexecute(),executeQuery() etexecuteUpdate() dans JDBC ?

Quelle est la différence entre les méthodesexecute(),executeQuery() etexecuteUpdate() dans JDBC ?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2023-09-17 14:33:12
avant
1859 Les gens l'ont consulté

JDBC 中的execute()、executeQuery() 和executeUpdate() 方法有什么区别?

Une fois que vous avez créé un objet instruction, vous pouvez l'exécuter à l'aide de l'une des méthodesexecute(),executeUpdate() etexecuteQuery() de l'interface Statement.

Méthodeexecute() : Cette méthode est utilisée pour exécuter des instructions SQL DDL. Elle renvoie une valeur booléenne spécifiant si l'objet ResultSet peut être récupéré.

Exemple

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

public class Example {

   public static void main(String args[]) throws SQLException {

      //Registering the Driver

      DriverManager.registerDriver(new com.mysql.jdbc.Driver());

      //Getting the connection

      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";

      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");

      System.out.println("Connection established......");

      //Creating the Statement

      Statement stmt = con.createStatement();

 

      //Executing the statement

      String createTable = "CREATE TABLE Employee( "

         + "Name VARCHAR(255), "

         + "Salary INT NOT NULL, "

         + "Location VARCHAR(255))";

      boolean bool = stmt.execute(createTable);

 

      System.out.println(bool);

   }

}

Copier après la connexion

Output

1

2

Connection established......

false

Copier après la connexion

executeUpdate() : Cette méthode est utilisée pour exécuter des instructions d'insertion, de mise à jour, de suppression et d'autres. Il renvoie une valeur entière représentant le nombre de lignes affectées.

Exemple

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

public class ExecuteUpdateExample {

   public static void main(String args[]) throws SQLException {

      //Registering the Driver

      DriverManager.registerDriver(new com.mysql.jdbc.Driver());

      //Getting the connection

      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";

      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");

      System.out.println("Connection established......");

      //Creating the Statement

      Statement stmt = con.createStatement();

      

      String insertData = "INSERT INTO Employee("

         + "Name, Salary, Location) VALUES "

         + "('Amit', 30000, 'Hyderabad'), "

         + "('Kalyan', 40000, 'Vishakhapatnam'), "

         + "('Renuka', 50000, 'Delhi'), "

         + "('Archana', 15000, 'Mumbai')";

 

      int i = stmt.executeUpdate(insertData);

      System.out.println("Rows inserted: "+i);

   }

}

Copier après la connexion

Output

1

2

Connection established......

Rows inserted: 4

Copier après la connexion

executeQuery() : Cette méthode est utilisée pour exécuter des instructions qui renvoient des données tabulaires (telles que select). Il renvoie un objet de la classe ResultSet.

Exemple

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class ExecuteQueryExample {

   public static void main(String args[]) throws SQLException {

      //Registering the Driver

      DriverManager.registerDriver(new com.mysql.jdbc.Driver());

      //Getting the connection

      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";

      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");

      System.out.println("Connection established......");

      //Creating the Statement

      Statement stmt = con.createStatement();

 

      //Retrieving data

      ResultSet rs = stmt.executeQuery("Select *from Employee");

 

      while(rs.next()) {

         System.out.print("Name: "+rs.getString("Name")+", ");

         System.out.print("Salary: "+rs.getInt("Salary")+", ");

         System.out.print("City: "+rs.getString("Location"));

         System.out.println();

      }

   }

}

Copier après la connexion

Sortie

1

2

3

4

5

Connection established......

Name: Amit, Salary: 30000, City: Hyderabad

Name: Kalyan, Salary: 40000, City: Vishakhapatnam

Name: Renuka, Salary: 50000, City: Delhi

Name: Archana, Salary: 15000, City: Mumbai

Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal