Java JDBC SQL SERVER:语句没有返回结果集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20669815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JDBC SQL SERVER: The statement did not return a result set
提问by user2966439
I am executing the following query from Microsoft SQL Server Studio, which works fine and displays results:
我正在从 Microsoft SQL Server Studio 执行以下查询,它工作正常并显示结果:
SELECT *
INTO #temp_table
FROM md_criteria_join
WHERE user_name = 'tecgaw'
UPDATE #temp_table
SET user_name = 'tec'
WHERE user_name != 'tec'
SELECT *
FROM md_criteria_join
WHERE user_name = 'tec'
AND view_name NOT IN (SELECT view_name
FROM md_criteria_join
WHERE user_name = 'tecgaw')
UNION
SELECT *
FROM #temp_table
ORDER BY view_name,
user_name,
crit_usage_seq,
crit_join_seq
However, if I execute the same query in Java, an Exception is thrown stating
但是,如果我在 Java 中执行相同的查询,则会抛出一个异常,说明
The statement did not return a result set.
该语句没有返回结果集。
Here's the Java code:
这是Java代码:
statement = conn.getConnection().createStatement();
resultSet = stmt.executeQuery(sql.toString());
Is that because I cannot do multiple SQL queries in one statement (I.e., Creating the #temp_table
, updating it, and then using for it my select statement)?
那是因为我不能在一个语句中执行多个 SQL 查询(即,创建#temp_table
、更新它,然后使用我的 select 语句)?
采纳答案by Antanas
I have found similar question in StackOverflow here. You should enable connection to support multiple statements and separate them using ;
. For concrete examples see that answer. However it is for MySql only.
我在这里的StackOverflow 中发现了类似的问题。您应该启用连接以支持多个语句并使用;
. 有关具体示例,请参阅该答案。但是,它仅适用于 MySql。
Also I think you can rewrite your SQL into single query
另外我认为您可以将 SQL 重写为单个查询
SELECT columnA, columnB, 'tec' as user_name from md_criteria_join
WHERE (
user_name = 'tec'
AND view_name NOT IN (
SELECT view_name
FROM md_criteria_join
WHERE user_name = 'tecgaw')
)
OR user_name = 'tecgaw'
ORDER BY view_name, user_name, crit_usage_seq, crit_join_seq
Another option is to move your statements to stored procedure and ivoke it from JDBC using CallableStatement
另一种选择是将您的语句移动到存储过程并使用 CallableStatement 从 JDBC 调用它
Or maybe you should try executing it with multiple jdbc statements like this
或者也许您应该尝试使用多个 jdbc 语句来执行它
Connection conn = conn.getConnection(); //just to make sure its on single connection
conn.createStatement("SELECT INTO #temp_table").executeUpdate();
conn.createStatement("UPDATE #temp_table").executeUpdate();
conn.createStatement("SELECT ...").executeQuery();
Note you have to close resources and maybe for better performance you could use addBatch and executeBatch methods
请注意,您必须关闭资源,也许为了获得更好的性能,您可以使用 addBatch 和 executeBatch 方法
回答by Ravindra Gullapalli
Use execute
statement for data manipulationlike insert, update and deleteand
executeQuery
for data retrievallike select
使用execute
for语句数据操作一样插入,更新和删除,并
executeQuery
进行数据检索喜欢选择
I suggest you to separate your program into two statements one execute
and one executeQuery
.
我建议你把你的程序分成两个语句一execute
和一executeQuery
。
If you do not wish to do that, try separating the statements with semi-colon. But I am not sure about this action if this gives you a resultset or not.
如果您不想这样做,请尝试用分号分隔语句。但是我不确定这个操作是否会给你一个结果集。
回答by John Gietzen
JDBC is getting confused by row counts.
JDBC 被行数弄糊涂了。
You need to use SET NOCOUNT ON
.
您需要使用SET NOCOUNT ON
.
回答by Suresh Gourishett
in ms sql you also have to do set nocount on right at the beginning of the stored procedure along with terminating select / update/ insert block statement with ";"
在 ms sql 中,您还必须在存储过程的开头设置 nocount,并使用“;”终止 select/update/insert 块语句。