问题
My original environment is SQL server 2005 + WebSphere v6.0(JDBC 3.0). When I run the program as below and it works well.
ResultSet rs=stmt.executeQuery(sql);
rs.next();
However, when I upgrade the environment to SQL server 2005 + WebSphere v8.5(JDBC 4.0), I get the error message:
com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
From this forum's information, it seems that I have multiple resultsets, so I try to change the program as follows and it works fine.
stmt.execute(sql);
stmt.getMoreResults();
stmt.getMoreResults();
ResultSet rs=stmt.getResultSet();
rs.next();
My questions is that is there any approach that I can keep my program unchanged and works well with JDBC 4.0 driver(WAS v8.5)
or any combination like SQL svr 2000 + WAS v8.5
, etc.
Please give me any pointer and your recommendation is valuable to me, thank you.
Ann
回答1:
You are attempting to execute a query that either produces multiple resultsets or that does not produce a resultset (eg UPDATE
, INSERT
etc) using executeQuery
. The Javadoc for this method explicitly says:
Throws: SQLException - if a database access error occurs, this method is called on a closed Statement, the given SQL statement produces anything other than a single ResultSet object
You either need to use the executeUpdate
method (if it is actually an update/insert/delete, or execute
and then use the resulting boolean
and that of getMoreResults() to decide how to proceed.
来源:https://stackoverflow.com/questions/14170467/java-exception-the-statement-didnt-return-a-resultset