2016-11-21 175 views
3

我試圖讓輸出參數傳遞到另一個SP,所以我創建了一個測試過,如果我能得到它的字符串兩個輸出參數和ResultSet值,而是一個異常被拋出:從SQL Server存儲過程

java.sql.SQLException中:無效狀態,該結果集對象關閉

沒有cs.getMoreResults();另一個拋出異常:

java.sql.SQLException:輸出參數尚未處理。調用getMoreResults()。

如果我確實刪除了我的if (rs.next()) {,那麼它可以工作。 如何獲取輸出參數和仍然使用我的if rs.next

protected String doInBackground(String... params) { 
     if (userid.trim().equals("Developer")|| password.trim().equals("Dev!n_234")) 
      isSuccess2=true; 
     z = getString(R.string.login_succes); 
     if(userid.trim().equals("")|| password.trim().equals("")) 
      z = getString(R.string.indsæt_rigtigt_bruger); 
     else { 
      try { 
       Connection con = connectionClass.CONN(); 
       if (con == null) { 
        z = getString(R.string.Forbindelses_fejl) + "L1)"; 

       } else { 
        String ID; 
        ID = setingPreferences.getString("companyid", ""); 
        CallableStatement cs = null; 
        String query = "{ call [system].[usp_validateUserLogin](?,?,?,?,?)} "; 
        cs = con.prepareCall(query); 
        cs.setString(1, userid); 
        cs.setString(2, password); 
        cs.setString(3, ID); 
        cs.setBoolean(4, true); 
        cs.registerOutParameter(5, Types.VARCHAR); 
        ResultSet rs = cs.executeQuery(); 

        cs.getMoreResults(); 
        System.out.println("Test : " + cs.getString(5)); 

        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)+"L2)"; 
       Log.e("MYAPP", "exception", ex); 
      } 
     } 
     return z; 

    } 
} 
+0

閱讀[Javadoc文檔'聲明#getMoreResults'(https://開頭的文檔.oracle.com/JavaSE的/ 8 /文檔/ API/JAVA/SQL/Statement.html#getMoreResults--): 「和隱式關閉所有當前ResultSet」 – bradimus

+0

嘗試調用'cs.execute()'(而不是'CS。 executeQuery()'),然後看看你是否可以從'cs.getString(5)'得到輸出參數值。 –

+0

我不能用'cs.execute',因爲它是不兼容的類型與我'的ResultSet RS =' 但如果我不刪除我的'如果(rs.next()){'然後它我究竟如何可以同時使用我的代碼中的東西? –

回答

1

你需要處理的ResultSet值(一個或多個),然後再檢索輸出參數值。這是因爲SQL Server將輸出參數發送結果集(參考:here)後的值

所以,這是不行的:

ResultSet rs = cs.executeQuery(); 
System.out.println(cs.getString(5)); // clobbers the ResultSet 
rs.next(); // error 

但是這應該沒問題:

ResultSet rs = cs.executeQuery(); 
if (rs.next()) { 
    // do stuff 
} 
System.out.println(cs.getString(5)); // retrieve the output parameter value 
+0

ARH好了,所以這是它的原因,因爲我一直想把它是有前 –