解决Can not issue executeUpdate() or executeUpdate() with statement that produce result sets问题~

简介: 解决Can not issue executeUpdate() or executeUpdate() with statement that produce result sets问题~

错误描述如下所示:

Can not issue executeUpdate() or executeUpdate() with statement that produce result sets:不能使用返回结果集的 SQL 语句执行 executeUpdate() 或 executeLargeUpdate() 方法。


这个错误通常由以下两种情况引起:


1.SQL 语句返回结果集:如果 SQL 语句会返回结果集(例如 SELECT 语句),则不能使用 executeUpdate() 或 executeLargeUpdate() 方法执行该语句,应该使用 executeQuery() 方法。


2.在同一 Statement 对象中执行多个 SQL 语句:如果在同一 Statement 对象中先执行一条返回结果集的 SQL 语句,然后再执行一条修改数据库内容的 SQL 语句,则会导致这个错误。


例如:

Statement stmt = conn.createStatement();
// 执行第一条 SQL 语句
ResultSet rs = stmt.executeQuery("SELECT * FROM user "); 
// 执行第二条 SQL 语句
int affectedRows = stmt.executeUpdate("DELETE FROM user WHERE id=1"); 


解决上述的这种情况,方法为:先创建多个 Statement 对象来执行不同的 SQL 语句,而不是在同一 Statement 中执行多个 SQL 语句。

相关文章
### Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have
### Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have
|
6月前
|
SQL 关系型数据库 MySQL
java.sql.SQLException: No operations allowed after statement closed.
java.sql.SQLException: No operations allowed after statement closed.
232 0
|
SQL 关系型数据库 数据库
DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null
DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null
353 0
[已解决]SpringDataJPA+Hibernate在执行executeUpdate()的时候报错 Executing an update/delete query
[已解决]SpringDataJPA+Hibernate在执行executeUpdate()的时候报错 Executing an update/delete query
|
数据库
Caused by: java.sql.SQLException: Table ‘.\***\*****‘ is marked as crashed and should be repaired
Caused by: java.sql.SQLException: Table ‘.\***\*****‘ is marked as crashed and should be repaired
105 0
Java.sql.SQLException: Illegal operation on empty result set.
Java.sql.SQLException: Illegal operation on empty result set.
269 0
|
数据库
java.sql.SQLException: Field ‘id‘ doesn‘t have a default value
java.sql.SQLException: Field ‘id‘ doesn‘t have a default value
|
Java 数据库连接 mybatis
A query was run and no Result Maps were found for the Mapped Statement
A query was run and no Result Maps were found for the Mapped Statement
220 0
|
Java 数据格式 XML
JakartaEE Exception: Invalid bound statement (not found): com.mazaiting.blog.dao.UserDao.selectUs...
异常 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.
1209 0