JDBC中的execute(),executeQuery()和executeUpdate()方法有什么区别?
一旦你创建了Statement对象可以使用的即声明接口的执行方法之一执行它,execute()
,executeUpdate()
和executeQuery()
。
的execute()
方法:该方法是用于执行SQL DDL语句,它返回一个布尔值,指定的天气ResultSet对象可以被检索。
示例
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 {
//注册驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获得连接
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//创建语句
Statement stmt = con.createStatement();
//执行语句
String createTable = "CREATE TABLE Employee( "
+ "Name VARCHAR(255), "
+ "Salary INT NOT NULL, "
+ "Location VARCHAR(255))";
boolean bool = stmt.execute(createTable);
System.out.println(bool);
}
}
输出结果
Connection established......false
executeUpdate():此方法用于执行诸如插入,更新,删除之类的语句。它返回一个整数值,表示受影响的行数。
示例
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 {
//注册驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获得连接
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//创建语句
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);
}
}
输出结果
Connection established......Rows inserted: 4
executeQuery():此方法用于执行返回表格数据的语句(示例select)。它返回ResultSet类的对象。
示例
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 {
//注册驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获得连接
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//创建语句
Statement stmt = con.createStatement();
//检索数据
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();
}
}
}
输出结果
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
以上是 JDBC中的execute(),executeQuery()和executeUpdate()方法有什么区别? 的全部内容, 来源链接: utcz.com/z/358074.html