execute
boolean execute()
throws SQLException
Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement. Some prepared statements return multiple results; the execute method handles these complex statements as well as the simpler form of statements handled by the methods executeQuery and executeUpdate.
The execute method returns a boolean to indicate the form of the first result. You must call either the method getResultSet or getUpdateCount to retrieve the result; you must call getMoreResults to move to any subsequent result(s).
Returns:
true if the first result is a ResultSet object; false if the first result is an update count or there is no result
Throws:
SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or an argument is supplied to this method
See Also:
Statement.execute(java.lang.String), Statement.getResultSet(), Statement.getUpdateCount(), Statement.getMoreResults()
----------------------------------------------------------------------
executeQuery
ResultSet executeQuery()
throws SQLException
Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
Returns:
a ResultSet object that contains the data produced by the query; never null
Throws:
SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement does not return a ResultSet object
----------------------------------------------------------------------
executeUpdate
int executeUpdate()
throws SQLException
Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.
Returns:
either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
Throws:
SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement returns a ResultSet object
分享到:
相关推荐
JDBC 中的 PreparedStatement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和 execute。这些方法的使用取决于 SQL 语句所产生的内容。 _executeQuery 方法_ executeQuery 方法用于产生单个...
在Java开发中,尤其涉及到数据库操作时,`execute`, `executeQuery` 和 `executeUpdate` 这三个方法是JDBC编程中非常重要的一部分。它们分别适用于不同类型的SQL语句,理解这些方法的区别对于正确高效地执行数据库...
在实际应用中,如果需要获取动态构建的SQL,可能需要自定义一个`PreparedStatement`的代理类,覆盖`execute`或`executeQuery`方法,然后在这个代理类中拼接和打印出最终的SQL。但这需要对JDBC有深入的理解,并且需要...
- `PreparedStatement`提供了修改后的`execute`, `executeQuery`和`executeUpdate`方法,这些方法不再接受SQL语句作为参数。 - 而是直接执行预编译过的SQL语句,提高了执行效率。 #### 三、创建`...
封装了java使用jdbc对mysql的操作,以及java使用jdbc对mysql的事务处理,对execute、executeUpdate、executeQuery进行了封装,把繁琐的创建数据库连接对象、PreparedStatement对象、结果集对象,打开关闭连接进行了...
- 执行SQL:调用Statement或PreparedStatement的`executeQuery()`或`executeUpdate()`方法。 - 处理结果:对于查询语句,通过ResultSet处理结果;对于更新操作,检查返回的受影响行数。 - 清理资源:关闭...
4. 调用 `PreparedStatement` 的 `executeUpdate()` 方法执行 SQL 语句。 5. 关闭 `PreparedStatement` 和 `Connection`。 #### 3.2 执行 DQL(查询) 1. 获取 `Connection` 连接。 2. 通过 `Connection` 创建 `...
- **查询**:创建一个PreparedStatement对象,使用`prepareStatement()`方法,然后调用`setXXX()`方法设置参数,最后执行`executeQuery()`获取结果集。 ```java String sql = "SELECT * FROM score WHERE number = ?...
4. 执行SQL:调用Statement或PreparedStatement的executeQuery()或executeUpdate()方法。 5. 处理结果:如果执行的是查询语句,需要遍历ResultSet对象获取结果。 6. 关闭资源:关闭Statement、ResultSet以及...
如同Statement,PreparedStatement也有执行SQL的方法,如`execute()`, `executeQuery()`, 和 `executeUpdate()`,但它们接受预编译的SQL语句和参数。在执行前,必须用setXXX()方法为每个参数提供值,否则会抛出...
对于Statement,使用executeQuery()或executeUpdate()方法。对于PreparedStatement,需要先设置参数,然后执行。例如,`int result = (Integer) pstmt.getMethod("executeUpdate").invoke(pstmt)`。 5. **处理结果...
设置了参数后,可以通过 `execute`、`executeQuery` 或 `executeUpdate` 方法来执行 SQL 语句: - `execute`:执行任意类型的 SQL 语句,返回一个布尔值,表示结果是否为 `ResultSet`。 - `executeQuery`:执行查询...
`PreparedStatement`也有自己的`executeQuery`、`executeUpdate`和`execute`方法。 - **示例代码**: ```java PreparedStatement pstmt = con.prepareStatement("INSERT INTO Table1 (col1, col2) VALUES (?, ?)...
例如,`executeSql()`方法可以用来执行任意SQL语句,`executeUpdate()`用于更新操作,`executeQuery()`用于查询,返回`ResultSet`。使用`PreparedStatement`代替`Statement`可以防止SQL注入,并提高代码的可读性和可...
`Statement`提供了多种执行SQL语句的方法,包括`executeQuery()`, `executeUpdate()` 和 `execute()`等。这些方法分别适用于不同类型的操作: - **`executeQuery()`**:用于执行返回结果集(`ResultSet`)的SQL语句...
4. **执行SQL语句/存储过程**: 使用Statement对象的executeQuery()或executeUpdate(),PreparedStatement的execute()方法。 5. **处理结果集**: 如果是查询操作,遍历ResultSet对象,获取数据。 6. **关闭资源**: ...
17. **execute()方法**: 当用户不知道执行 SQL 语句后会产生什么结果时,可以调用 execute() 方法。 #### 十八至二十、获取不同类型的结果 18. **getInt()方法**: 用于返回当前行第 X 列的值,类型为 int。 19. **...
4. PreparedStatement:是 Statement 的子接口,允许数据库预编译 SQL,避免数据库每次重新编译,性能较好。常用方法有: - executeQuery():执行查询,返回结果集对应的 ResultSet 对象。 - executeUpdate():...
在本教程中,我们将深入探讨PreparedStatement和CallableStatement接口,以及Oracle数据库中处理LOB数据的方法。 1. PreparedStatement接口 PreparedStatement接口是为了提高SQL语句执行的效率和安全性而设计的。当...
4. 执行SQL:调用对象的executeQuery()或executeUpdate()方法执行SQL。 5. 处理结果:如果执行的是查询,获取ResultSet对象并遍历数据。 6. 关闭资源:关闭ResultSet、Statement和Connection对象,释放资源。 在...