george
george

Reputation: 3221

jdbc ResultSet is null

I am trying to run some updates and eventually return some resultSet which I can consume but the result is always null. My code is

PreparedStatement preparedStatement = aConnection.prepareStatement(script);
boolean results = preparedStatement.execute();

If my query is

 BEGIN                  
  Declare @MyLog as varchar(max) = '---------------------- ' 
         +  CHAR(10) + CHAR(10)              
    BEGIN         
      BEGIN TRAN    

        UPDATE MPRN SET K4101 = 'N'
        select * from HELD_FLOW

        COMMIT TRAN 

    END                  
END

the statement doesn't return any values. However, if my query is

 BEGIN                  
  Declare @MyLog as varchar(max) = '---------------------- ' 
         +  CHAR(10) + CHAR(10)              
    BEGIN         
      BEGIN TRAN    
        select * from HELD_FLOW
        UPDATE MPRN SET K4101 = 'N'


        COMMIT TRAN 

    END                  
END

all values are returned! So the order of update and select matters! I want to update some values first and finally return some output as in the 1st example which doesn't work...

Upvotes: 2

Views: 283

Answers (1)

Timekiller
Timekiller

Reputation: 3136

It's all in the javadoc:

The execute method executes an SQL statement and indicates the form of the first result. You must then use the methods getResultSet or getUpdateCount to retrieve the result, and getMoreResults to move to any subsequent result(s).

Your procedure returns multiple results, execute shows you the type of first one. In first example, first result is an update count, in second one it's a result set, so you're getting false and true respectively.

You should use getMoreResults to process the remaining results and find the ResultSet.

Upvotes: 3

Related Questions