What is the difference between execute(), executeQuery() and executeUpdate() methods in JDBC?

WBOY
Release: 2023-09-17 14:33:12
forward
1232 people have browsed it

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

Once you create a statement object, you can execute it using one of the execute(), executeUpdate(), and executeQuery() methods of the Statement interface.

execute() method:This method is used to execute SQL DDL statements. It returns a Boolean value specifying whether the ResultSet object can be retrieved.

Example

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); } }
Copy after login

Output

Connection established...... false
Copy after login

executeUpdate():This method is used to execute insert, update, delete and other statements. It returns an integer value representing the number of rows affected.

Example

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); } }
Copy after login

Output

Connection established...... Rows inserted: 4
Copy after login

executeQuery():This method is used to execute statements that return table data (such as select). It returns an object of ResultSet class.

Example

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(); } } }
Copy after login

Output

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
Copy after login

The above is the detailed content of What is the difference between execute(), executeQuery() and executeUpdate() methods in JDBC?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!