2

i have an issue where i trying to use a sp but i be getting a

The executeQuery method must return a result

and if i use cs.execute(); with ResultSet rs = cs.getResultSet(); instead i get a nullpointer on if(rs.next())

protected String doInBackground(String...params) {
    if (userid.trim().equals("") || password.trim().equals("")) z = getString(R.string.wrong_user);
    else {
        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = getString(R.string.connection_error);
            } else {
               String query = "{?=call [system].[usp_validateUserLogin](?,?,?,?,?)}";
                    CallableStatement cs = con.prepareCall(query);
                    cs.registerOutParameter(1,Types.INTEGER);
                    cs.setString(2, userid);
                    cs.setString(3, password);
                    cs.setInt(4, 72);
                    cs.setNull(5, Types.BIT);
                    cs.registerOutParameter(6, Types.VARCHAR);
                    cs.execute();
                    boolean firstResultIsResultSet = cs.execute();
                    if (firstResultIsResultSet) {
                        ResultSet rs = cs.getResultSet();
                        // process result set
                    }
                }
            }
                if (rs.next()) {
                    z = getString(R.string.login_succes);
                    isSuccess = true;
                } else {
                    z = getString(R.string.Invalid_Credentials);
                    isSuccess = false;
                }
            }
        } catch (Exception ex) {
            isSuccess = false;
            z = getString(R.string.Exceptions);
        }
    }
    return z;
}

SP:

ALTER PROCEDURE [system].[usp_validateUserLogin]
    @p_Login                NVARCHAR ( 50 ),
    @p_Password             NVARCHAR ( 32 ),
    @p_CompanyID            INT,
    @p_OutDetails           BIT = 1,
    @p_AuthenticationTicket VARCHAR(200) OUTPUT
AS
  BEGIN
      SET NOCOUNT ON;

      DECLARE @errNo    INT,
              @recCount INT,
              @res      INT

      SELECT u.*
      INTO   #TMPLOGIN
      FROM   SYSTEM.[user] AS u WITH ( NOLOCK )
      WHERE  ( u.login = @p_Login )
             AND ( u.company_id = @p_CompanyID )
             AND ( PWDCOMPARE (@p_Password, u.passwd) = 1 )
             AND ( u.status = 0 ) --Active
      SELECT @errNo = @@ERROR,
             @recCount = @@ROWCOUNT

      IF ( @errNo <> 0 )
        BEGIN
            RETURN 1010
        END

      IF ( @recCount = 1 )
        BEGIN
            DECLARE @userID INT

            SELECT @userID = id
            FROM   #TMPLOGIN

            EXEC @res = SYSTEM.USP_RENEWAUTHENTICATIONTICKET
              @p_DoerTicket = '',
              @p_AuthenticationTicket = @p_AuthenticationTicket OUTPUT,
              @p_UserID = @userID,
              @p_CompanyID = @p_CompanyID

            IF ( @res <> 0 )
              RETURN @res
        END

      --SET @p_AuthenticationTicket = 'TESTAUTHENTICATIONTICKET0123456789'
      IF ( @p_OutDetails = 1 )
        BEGIN
            SELECT *
            FROM   #TMPLOGIN
        END

      RETURN 0
  END  

enter image description here

  • Does that stored procedure return a result set, or does it have `OUT` parameters? You might also want to try using `set nocount on` in your stored procedure (it might return an update count first). Also add the code for that stored procedure to your question, eg see http://stackoverflow.com/questions/24428928/jdbc-sql-error-statement-did-not-return-a-result-set – Mark Rotteveel Oct 31 '16 at 09:07
  • it has out param and nocount is on – Kewin Björk Nielsen Oct 31 '16 at 09:12
  • The problem is that this stored procedure has **three** different ways of communicating information back: an out parameter, a return value **and** a result set. The order of those results depends on the logic in your stored procedure. – Mark Rotteveel Oct 31 '16 at 09:17
  • if i understand this sp right it first gives a return value out and result set so i am not able to get it to Work with the way my structure is? if so are there any other way around to check if the user had been logged in? – Kewin Björk Nielsen Oct 31 '16 at 09:25

1 Answers1

3

Warning: this answer was written without access to a SQL Server instance, so it might not be correct, I hope it will help you. I'll try to update it later when I do have access to an SQL Server system.

The problem seems to be is that you have three different ways of getting values from this stored procedure. You have a return value, an OUTPUT parameter and a result set. If the result set is produced depends on the logic of your stored procedure, so you must be prepared to handle its absence.

First of all, to get the return value of the stored procedure, you must use:

String query = "{?=call [system].[usp_UserLogin(?,?,?,?,?)}";

Where the ?= is the return value.

This also has an effect on the the index of the various parameters. It may also be necessary to explicitly register the first (return) parameter as an out parameter.

Then you need to execute the stored procedure and be prepared to handle the absence of a result set:

boolean firstResultIsResultSet = cs.execute();
if (firstResultIsResultSet) {
    ResultSet rs = cs.getResultSet();
    // process result set
}

Note that if you would have multiple result sets and also update counts, this would get even more complicated.

You should be able to get the return value using cs.getInt(1), although I'm not 100% sure if you can get it before checking and processing the result set. On this the Javadoc for CallableStatement says:

For maximum portability, a call's ResultSet objects and update counts should be processed prior to getting the values of output parameters.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • sorry i am not 100% sure if i understand this right as i a had mostly worked with more basic SP for filtering and search before. so is `cs.registerOutParameter(1,Types.INTEGER); cs.setString(2, userid);` but i gets a java.sql.SQLException: Parameter #7 has not been set. what is the 7th param? and what should i use in the`firstResultIsResultSet = cs.execute(); if (firstResultIsResultSet) { ResultSet rs = cs.getResultSet(); ` is that there i should use my issucess= true and false instead of `if rs.next`? – Kewin Björk Nielsen Oct 31 '16 at 16:20
  • or is that what you mean with cs.getInt(1) where i can use the return value something like this `if = 0 issucess= true else if issuces=false`? to make it more logical? as showed in the picture i had uploaded – Kewin Björk Nielsen Nov 01 '16 at 09:21
  • @KewinBjörkNielsen I'll need to reproduce this before I can give answer that first comment. As to your second comment, according to the stored procedure shown, the success should be derived from the return value, not the result set. – Mark Rotteveel Nov 01 '16 at 09:33
  • alright let me know if you need some more info on the other procedures such as sign up, renew authenticationticket, and validate authenticationticket – Kewin Björk Nielsen Nov 01 '16 at 10:00
  • i still haven't figured out what the 7th param is – Kewin Björk Nielsen Nov 12 '16 at 17:46