Java中 Execute()、query() 和 Update() 方法的区别
在开始之前,让我们先了解一下参数 使用以下三个查询参数,如下所示:
- boolean execute(String SQL):如果可以检索到 ResultSet 对象,则返回布尔值 true;否则,它返回 false。使用此方法执行 SQL DDL 语句或需要使用真正动态 SQL 时。
- int executeUpdate(String SQL):返回受SQL语句执行影响的行数。使用此方法来执行 SQL 语句,您希望得到一些受影响的行 - 例如,INSERT、UPDATE 或 DELETE 语句。
- ResultSet executeQuery(String SQL):返回一个 ResultSet 对象。当您希望获得结果集时使用此方法,就像使用 SELECT 语句一样。
它们以下面列出的方式讨论如下:
- 执行()
- 执行查询()
- 执行更新()
方法一: execute()
- 说明:该方法用于所有类型的 SQL 语句,即返回布尔值 TRUE 或 FALSE。
- 返回类型:此方法返回一个布尔值。 TRUE 表示查询返回了一个结果集对象,FALSE 表示返回了一个 int 值或什么都不返回。
- 用法:此方法用于执行选择和非选择查询。
- 示例:所有 SQL 语句。
插图:
Java
// Java Program to Illustrate usage of execute() Method
// Loading the driver using forName() method
Class.forName(driver);
// Registering the driver using Drivermanager.getConnection() method
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "1234");
// Get database connection
stmt = conn.createStatement();
// Use Connection to create a Statement object
// Execute SQL and return boolean value to
// indicate whether it contains ResultSet
boolean hasResultSet = stmt.execute(sql);
// Condition holds true till there is a single element
if (hasResultSet)
{
// If there is a ResultSet result set after execution
rs = stmt.getResultSet();
// Get the result set
ResultSetMetaData rsmd = rs.getMetaData();
// ResultSetMetaData is a metadata interface for analyzing result sets
int columnCount = rsmd.getColumnCount();
// Getting the output ResultSet object
// with help of object of ResultSet
while (rs.next ())
{
for (int i = 0 ; i < columnCount ; i++ )
{
System.out.print(rs.getString(i + 1) + "/t");
}
System.out.print("/n");
}
}
else
{
System.out.println ("The records affected by this SQL statement are"
+ stmt.getUpdateCount () + "Article");
}
Java
// Java Program to Illustrate execute Query() Method
// Again first step is to load and register drivers
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root","root");
// Using DriverManager to get database connection
Statement stmt = conn.createStatement();
// Use Connection to create a Statement object
// Creating an object of ResultSet class
ResultSet rs =stmt.executeQuery("select * from teacher");
// Execute the query statement and save the result
// Iterating over elements in above object
while (rs.next())
{
// Getting the output the query result
System.out.println(rs.getInt(1) + "/t" + rs.getString(2));
}
方法二:执行Query()
- 描述:现在这个方法通过从数据库中获取一些数据来执行返回结果集的语句。
- 用法:此方法用于执行选择查询。
- 返回类型:此方法返回一个 Result Set 对象,其中包含查询返回的结果。
- 其中一个很常见的例子是:'SELECT'
插图:
Java
// Java Program to Illustrate execute Query() Method
// Again first step is to load and register drivers
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root","root");
// Using DriverManager to get database connection
Statement stmt = conn.createStatement();
// Use Connection to create a Statement object
// Creating an object of ResultSet class
ResultSet rs =stmt.executeQuery("select * from teacher");
// Execute the query statement and save the result
// Iterating over elements in above object
while (rs.next())
{
// Getting the output the query result
System.out.println(rs.getInt(1) + "/t" + rs.getString(2));
}
方法三:执行Update()
- 说明:此方法用于执行 DML 语句(INSERT、UPDATE 和 DELETE),该语句返回 int 值,受影响的行数。
- 用法:此方法用于执行非选择查询。此方法用于执行选择和非选择查询。
- 返回类型:一个整数值,表示受查询影响的行数。对于不返回任何内容的语句,这将是 0。
- 例子:
DML->INSERT , UPDATE and DELETE
DDL-> CREATE, ALTER
插图:
Class.forName("com.mysql.jdbc.Driver");
// Load the database driver
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root","1234");
// Use DriverManager to get database connection
Statement stmt = conn.createStatement();
// Use Connection to create a Statement object
return stmt.executeUpdate(sql);
// Execute the DML statement and return the number of records affected
Now let us finally conclude out the differences in return types as spotted in the above illustrations
- execute(): The return type is Boolean, indicating whether ResultSet return
- executeQuery(): Type method returns a ResultSet, execute returns the results of the query, often used to perform the query
- executeUpdate(): The return type is int, that the implementation of a number of rows affected after the sql statement, usually used to execute modification statements.