Domanda

I've been searching google and stack overflow for the past several hours, and can't seem to find an exact match on this issue. I'm relatively new to java, so please correct me if I'm just doing something incredibly silly!

Issue: When executing a try-with-resource query (statement.executeQuery(stmt)) I am getting a 'Statement did not return a result set'

I can guarantee this is valid SQL, and when run through SSMS the query works - and when using the select...into...from style syntax, this query works as well [in java]. The select...into...from is not a viable option though as it creates performance issues (23 second query turns into a 5+ minute query due to resource utilization)

Proof of concept: SQL file: c:\test.sql

set nocount on
create table #test (loannum int)
insert into #test (loannum)
select 1
select * from #test
drop table #test

This query is used to make a cached result set so that I can pass the data around other functions without leaving an open connection to the database...

Note: SQLInfo in the below is just "C:\test.sql" < snip>

try(BufferedReader in = new BufferedReader(new FileReader(SQLInfo)))
                {
                    String str;
                    StringBuffer sb = new StringBuffer();
                    while((str = in.readLine()) != null)
                    {
                        sb.append(str + System.getProperty("line.separator"));
                    }

                    _stmt = sb.toString();
                }

< /snip>

which is later used in the below... < snip>

try (Connection connection = DriverManager.getConnection(CONNECTION);
                Statement statement = connection.createStatement();
                ResultSet resultset = statement.executeQuery(_stmt)
            ) 
        {

            crs.populate(resultset);

            return crs;
        }
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
        return crs;

< /snip>

What I'm expecting, is a cached result set that will be used in a defaulttablemodel to display a "loannum" field, and value of 1 -- what I really get is the following error: "com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set..."

If I modify the SQL to the following:

set nocount on
select 1 as loannum
into #test
select * from #test
drop table #test

I get the expected return result of "loannum" with value of 1.

It appears to have something to do with the create table statement in the executeQuery method, but it's a temporary table! Is there any way to make the above to work with the create table (value type, value type, value type) insert into... method?

**Note: I've attempted to use the .execute(String) method, and parse through the return results. It appears to have the same issue, but it's entirely possible I'm implementing it incorrectly.

**Secondary note: The defaulttablemodel works fine, and returns information to a JTable that displays on screen. This dynamically allocates the column names, and displays the associated values. I can provide a sample of this code if needed, but i don't think it's relevant to the issue as this is failing on simply creating the resultset, rather then when it's trying to assign the information to the table model.

[Editing this in]: Stored procedures on SQL side are not a viable option in this instance. This is a query against a data warehouse that does not allow stored procs as company policy (I've pushed back, and have been smacked down) - As a result, I'm working on this ... hackish solution.

Hopefully somebody has some insight into this issue!

È stato utile?

Soluzione

As it turns out, after I updated to use the execute command, I had accidentally included it into a try-with-resources block, resulting in returning a closed dataset to the calling function, which would then error with an empty resultset passed to create the default table model. Whoops.

Ultimately, the execute query worked with the following (refactoring and cleanup still necessary, this was just 'get it working'):

try 
        {
            boolean hasResults = statement.execute(_stmt);

            do { //I think... this could only possibly get one result. FIXME

            if (hasResults) 
            {
                try(ResultSet rs = statement.getResultSet())
                {
                    CachedRowSetImpl crs = new CachedRowSetImpl();
                    crs.populate(rs);

                    return crs;
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            hasResults = statement.getMoreResults();

            } while (hasResults || statement.getUpdateCount() != -1);

            } 
        catch (Exception e) 
        {
            e.printStackTrace();
        } 
        finally 
        {
            statement.close();
        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top