JDBC SQL SERVER: The statement did not return a result set

user2966439 picture user2966439 · Dec 18, 2013 · Viewed 49.1k times · Source

I am executing the following query from Microsoft SQL Server Studio, which works fine and displays results:

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

The statement did not return a result set.

Here's the Java code:

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)?

Answer

John Gietzen picture John Gietzen · Mar 7, 2015

JDBC is getting confused by row counts.

You need to use SET NOCOUNT ON.