1. 程式人生 > >idea 創建運行web項目時,報錯: Can not issue executeUpdate() for SELECTs解決方案

idea 創建運行web項目時,報錯: Can not issue executeUpdate() for SELECTs解決方案

返回 定義 finall code 遇見 trac close nec ted

最近在做一個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; // 沒有其它結果

idea 創建運行web項目時,報錯: Can not issue executeUpdate() for SELECTs解決方案