簡體   English   中英

從SQL Server存儲過程中獲取輸出參數和ResultSet值

[英]get both output parameter and ResultSet values from SQL Server stored procedure

我試圖獲取輸出參數以傳遞到另一個SP中,所以我也創建了一個測試,看看是否可以從中獲取字符串,但會引發異常:

java.sql.SQLException:無效狀態,ResultSet對象已關閉

沒有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;

    }
}

您需要先處理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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM