Maison> base de données> tutoriel mysql> le corps du texte

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

WBOY
Libérer: 2023-09-17 14:33:12
avant
1228 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

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

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

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

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

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

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!

source:tutorialspoint.com
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
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!