Can not issue executeUpdate() for SELECTs

2023-10-18 08:58
文章标签 issue executeupdate selects

本文主要是介绍Can not issue executeUpdate() for SELECTs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在做一个Web课程设计的时候遇到了如下的问题。

java.sql.SQLException: 
java.lang.RuntimeException: java.sql.SQLException: Can not issue executeUpdate() for SELECTs
     at com.infuze.service.subscription.workflow.SyncSubscriptionTrackerWorkflow.executeProcess(SyncSubscriptionTrackerWorkflow.java:130)
     at com.infuze.service.workflow.WorkflowExecutor.execute(WorkflowExecutor.java:24)
     at com.infuze.service.subscription.xml.SubscriptionXmlService.syncTracker(SubscriptionXmlService.java:140)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at Caused by: java.sql.SQLException: Can not issue executeUpdate() for SELECTs
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2373)

 

不知道很多像我一样的新手有没有遇见同样的问题。

 

这个问题我还说不清楚是怎么回事?反正出现问题的代码样例如下:

 

Example Code

public SubscriptionDto getSubscription(String subscriptionGuid) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Exception ex = null;

SubscriptionDto subDto = null;
try {
log.debug("subscription guid" + subscriptionGuid);
conn = dataSource.getConnection();
ps = conn
.prepareStatement("select id,created,updated,access_token,token_secret,status,guid,service_id from subscriptions subs where subs.guid = ?");
ps.setString(1, subscriptionGuid);

ResultSet rst = ps.executeQuery();
while (rst.next()) {
subDto = new SubscriptionDto();
subDto.setId(rst.getLong("id"));
subDto.setCreated(rst.getDate("created"));
subDto.setUpdated(rst.getDate("updated"));
subDto.setAccessToken(rst.getString("access_token"));
subDto.setAccessTokenSecret(rst.getString("token_secret"));
subDto.setStatus(rst.getString("status"));
subDto.setGuid(rst.getString("guid"));
subDto.setServiceId(rst.getInt("service_id"));
}

ps.executeUpdate(); //  problem is here

log.debug("tracker updated successfully");

} catch (SQLException e) {
ex = e;
log.error("sql error occured while updating  subscription tracker" + subscriptionGuid, e);
} catch (Exception e) {
ex = e;
log.error("error occured  while updating subscription trakcer" + subscriptionGuid, e);
} finally {
closeDBObjects(conn, ps, rs);
checkException(ex);
}

return subDto;
}

注意以上加黑的部分,问题就出现在这里。

 

解决方案:用ps.execute();代替ps.executeUpdate.至于原因呢,

 

 

来看一下他们之间的区别:

Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和 execute。使用哪一个方法由 SQL 语句所产生的内容决定。

        方法executeQuery 
用于产生单个结果集的语句,例如 SELECT 语句。 被使用最多的执行 SQL 语句的方法是 executeQuery。这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL 语句。

        方法executeUpdate
用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。

        使用executeUpdate方法是因为在 createTableCoffees 中的 SQL 语句是 DDL (数据定义语言)语句。创建表,改变表,删除表都是 DDL 语句的例子,要用 executeUpdate 方法来执行。你也可以从它的名字里看出,方法 executeUpdate 也被用于执行更新表 SQL 语句。实际上,相对于创建表来说,executeUpdate 用于更新表的时间更多,因为表只需要创建一次,但经常被更新。


        方法execute:
        用于执行返回多个结果集、多个更新计数或二者组合的语句。因为多数程序员不会需要该高级功能

execute方法应该仅在语句能返回多个ResultSet对象、多个更新计数或ResultSet对象与更新计数的组合时使用。当执行某个已存储过程 或动态执行未知 SQL 字符串(即应用程序程序员在编译时未知)时,有可能出现多个结果的情况,尽管这种情况很少见。
        因为方法 execute 处理非常规情况,所以获取其结果需要一些特殊处理并不足为怪。例如,假定已知某个过程返回两个结果集,则在使用方法 execute 执行该过程后,必须调用方法 getResultSet 获得第一个结果集,然后调用适当的 getXXX 方法获取其中的值。要获得第二个结果集,需要先调用 getMoreResults 方法,然后再调用 getResultSet 方法。如果已知某个过程返回两个更新计数,则首先调用方法 getUpdateCount,然后调用 getMoreResults,并再次调用 getUpdateCount。
        对于不知道返回内容,则情况更为复杂。如果结果是 ResultSet 对象,则方法 execute 返回 true;如果结果是 Java int,则返回 false。如果返回 int,则意味着结果是更新计数或执行的语句是 DDL 命令。在调用方法 execute 之后要做的第一件事情是调用 getResultSet 或 getUpdateCount。调用方法 getResultSet 可以获得两个或多个 ResultSet 对象中第一个对象;或调用方法 getUpdateCount 可以获得两个或多个更新计数中第一个更新计数的内容。
        当 SQL 语句的结果不是结果集时,则方法 getResultSet 将返回 null。这可能意味着结果是一个更新计数或没有其它结果。在这种情况下,判断 null 真正含义的唯一方法是调用方法 getUpdateCount,它将返回一个整数。这个整数为调用语句所影响的行数;如果为 -1 则表示结果是结果集或没有结果。如果方法 getResultSet 已返回 null(表示结果不是 ResultSet 对象),则返回值 -1 表示没有其它结果。也就是说,当下列条件为真时表示没有结果(或没有其它结果): 

((stmt.getResultSet() == null) && (stmt.getUpdateCount() == -1))

如果已经调用方法 getResultSet 并处理了它返回的 ResultSet 对象,则有必要调用方法 getMoreResults 以确定是否有其它结果集或更新计数。如果 getMoreResults 返回 true,则需要再次调用 getResultSet 来检索下一个结果集。如上所述,如果 getResultSet 返回 null,则需要调用 getUpdateCount 来检查 null 是表示结果为更新计数还是表示没有其它结果。

        当 getMoreResults 返回 false 时,它表示该 SQL 语句返回一个更新计数或没有其它结果。因此需要调用方法 getUpdateCount 来检查它是哪一种情况。在这种情况下,当下列条件为真时表示没有其它结果: 

((stmt.getMoreResults() == false) && (stmt.getUpdateCount() == -1))

下面的代码演示了一种方法用来确认已访问调用方法 execute 所产生的全部结果集和更新计数: 


stmt.execute(queryStringWithUnknownResults);
while (true) {
int rowCount = stmt.getUpdateCount();
if (rowCount > 0) { // 它是更新计数
System.out.println("Rows changed = " + count);
stmt.getMoreResults();
continue;
}
if (rowCount == 0) { // DDL 命令或 0 个更新
System.out.println(" No rows changed or statement was DDL
command");
stmt.getMoreResults();
continue;
}

// 执行到这里,证明有一个结果集
// 或没有其它结果

ResultSet rs = stmt.getResultSet;
if (rs != null) {
. . . // 使用元数据获得关于结果集列的信息
while (rs.next()) {
. . . // 处理结果
stmt.getMoreResults();
continue;
}
break; // 没有其它结果

这篇关于Can not issue executeUpdate() for SELECTs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/231539

相关文章

新gre issue写作破题方法揭秘

以下是小编为大家搜索整理的有关新gre issue写作破题方法揭秘,ETS对于新gre issue写作高分作文有一个很重要也是最基本的要求,那就是complexity,也就是“立场和角度的多样化”,而新gre考试实施以来要很多同学对写作更加茫然,下面破题方法的全面解析:   写作是一个长期积累的过程,而gre写作一直是我们国内考生的一个薄弱环节,2011新gre考试实施以来,让很多更加茫然无

GRE写作——issue写作流程

无疑,GRE的写作是留学考试中难度最大的,也是需要我们(也包括母语为英语的大学生去准备。GRE issue写作与托福最大的区别在于“分析性”与“思辨性”。虽然官方指南上明确写出“不会因为学员受过特殊的大学写作训练而有特别的对待”,但是由于我们大学普遍没有开设分析性写作的课程,很多考生对于这个领域还是存在非常大的误解。这也许可以解释为什么很多人参加培训班、模仿网上盛传的作文思路最后得分依旧低于3

GRE Issue写作范文Topic123

Issue-123   题目:   The best way for a society to prepare its young people for leadership in government, industry, or other fields is by instilling in them a sense of cooperation, not competitio

新GRE issue写作范文实例

以下是关于传统与现实的新GRE issue写作范文实例,通过这些GRE写作范文或是习作,考生可以学习一下里面的短语、句子或思路,给自己的写作找一些思路和灵感。   题目:   Tradition and modernization are incompatible. One must choose between them.   传统和现代化是水火不容的。人们必须二者选一。   正文:

少之又少的新GRE issue写作范文

以下是关于选择少之又少的新GRE issue写作范文实例,通过这些GRE写作范文或是习作,考生可以学习一下里面的短语、句子或思路,给自己的写作找一些思路和灵感。   题目:   The absence of choice is a circumstance that is very, very rare.   没有选择的情况少之又少。   正文:   Admittedly being

关于“政府和艺术”的GRE Issue原题

双11还在刷屏购物吗,抽空和GRE频道一起来看看【关于“政府和艺术”的GRE Issue原题】   82"Government should never censor the artistic works or historical displays that a museum wishes to exhibit."   101"Governments should provide fun

【FlinkX】两个issue分析:reader和writer的通道数不一致+获取JobId

文章目录 issue详情reader和writer的通道数不一致获取JobId 代码分析#issue145配置说明源码分析: #issue148 最近准备再花点时间优化一下之前的FlinkX版本,特地去看了一下项目的issues区域,发现两个自己比较关注的issue。 issue详情 reader和writer的通道数不一致 异构数据源reader和writer设置不

Can not issue data manipulation statements with executeQuery().

这里的报错信息现实的如下图: 、 就是如图的两端代码的问题,excuteQuery是查询语句,而我要调用的是更新的语句 executeQurery()改成excuteUpdate() 错误就改正了

/etc/issue

/etc/issue里面命令的描述: vim /etc/issue Ubuntu 12.04.3 LTS  \n \l issue 内的各代码意义 \d 本地端时间的日期; \l 显示第几个终端机接口;  \m 显示硬件的等级 (i386/i486/i586/i686...);  \n 显示主机的网络名称;  \o 显示 domain name; \r 操作系统的版本 (相当于 unam

weditor安装时提示This is an issue with the package mentioned above, not pip

报错如下: note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for