【问题标题】:Unexpected parameter marker when trying to run my stored procedure尝试运行我的存储过程时出现意外的参数标记
【发布时间】:2016-10-28 09:22:52
【问题描述】:

所以我得到了一个

位置 40 出现意外的参数标记

当我尝试运行我的存储过程时 这是

   BEGIN //line 40 
   RETURN 1010
   END

我不确定那个是做什么的,以及它是否与我在代码中标记值的方式有关 代码

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 query = "{call [system].[usp_validateUserLogin](?,?,?,?,?)}";
                    CallableStatement ps = con.prepareCall(query);
                    ps.setString(1, userid);
                    ps.setString(2, password);
                    ps.setInt(3,72);
                    ps.setNull(4, Types.BOOLEAN);
                    ps.registerOutParameter(5, Types.VARCHAR);
                    ps.executeUpdate();
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    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;

    }
}
}

存储过程

    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

我的旧代码

protected String doInBackground(String... params) {
if(userid.trim().equals("")|| password.trim().equals(""))
z = "Please enter User Id and Password";
else
{
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select * from Usertbl where UserId='" + userid + "' and password='" + password + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

if(rs.next())
{

z = "Login successfull";
isSuccess=true;
}
else
{
z = "Invalid Credentials";
isSuccess = false;
}

}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions";
}
}
return z;
}
}

【问题讨论】:

  • 不确定您的 SP 中是否有任何错误,但我更喜欢将我的返回代码保存在一个 RC 变量中,并仅在返回此 RC 的 SP 结束时退出。
  • SP 中不应该有任何错误,因为它正在我们的主程序上运行
  • 我假设“位置 40”是查询字符串中的字符位置(括号开始的位置)。这肯定是客户端错误,您的 sp 未执行。请注意,您在调用prepareCall 之前调用了executeQuery

标签: java android sql-server stored-procedures


【解决方案1】:

这里似乎有些问题。正如 Ivan Starostin 指出的那样,您在 prepareCall 之前调用 executeQuery。另外:

CallableStatement cs = null; // This variable is cs
String query = "{call [system].[usp_validateUserLogin] (?,?,?,?,?)}";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query); // I assume the error is here because you're executing an empty statement
CallableStatement ps = con.prepareCall(query); // Then you make another CallableStatement called ps
ps.setString(1, userid);
// other stuff with ps
cs.executeUpdate(); // Then you ignore ps and call cs, which is null

作为程序员,您需要具备的一项技能是能够逐行查看代码并弄清楚它在做什么。如果您仔细查看了代码,您应该已经看到了这些问题。我不是刻薄的意思,只是想让大家想想以后这些问题怎么处理,因为它们会一遍遍地出现。每个人都会犯编码错误,这没什么大不了的,我们只需要学习如何在出现问题时发现它们。

【讨论】:

  • 我试图解决它并将 cs 更改为 ps 给了我同样的错误,并在执行更新给了我空指针或仅使用 ResultSet rs = ps.executeQuery(query); 后将 Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); 移动到使用 executeQuery这种类型的语句不支持(字符串)方法,所以我在这里有点迷失了,问题是我让它在我的旧代码中运行,没有任何我输入语句的问题,但我试图从我们的 sp我不知道如何让它与我的结构一起工作
  • 听起来你只是在尝试一些东西而不了解它们的作用。你知道你方法中的每一行是做什么的吗?
猜你喜欢
  • 2023-03-15
  • 2023-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多