简体   繁体   English

如何从Java调用MSSQL存储过程

[英]How to call a MSSQL stored procedure from java

I am trying to call a stored procedure from java and I'm having some issues getting the call to go through correctly. 我正在尝试从Java调用存储过程,但在使调用正确进行时遇到一些问题。 I've looked up numerous examples, but they don't match the syntax of the stored procedure I'm trying to call. 我查找了许多示例,但它们与我尝试调用的存储过程的语法不匹配。

    STORED PROCEDURE CODE
    ------------------------
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[getMgr]
    @UserID as varchar(5),
    @ModeType as char(1)
    AS
    BEGIN
    SELECT usermgrid from AllEmployees WHERE (ModeType = @ModeType AND UserID = 
    @UserID)
    END
JAVA CODE
----------------------------
    connection = getConnection();
    cs = connection.prepareCall("{call lvhsp_GetManager(?, ?) }");
    cs.setString(0, someUserID);
    cs.setString(1, "0");
    ResultSet resultSet = cs.executeQuery();

I've tried calling the stored procedure a number of different ways but I continue to get errors. 我曾尝试以多种不同的方式调用存储过程,但是我仍然遇到错误。

When I call it like this, I get a: 当我这样称呼它时,我得到:

com.microsoft.sqlserver.jdbc.SQLServerException: The index 0 is out of range. com.microsoft.sqlserver.jdbc.SQLServerException:索引0超出范围。

Which I assume is because that is not the correct way to pass a parameter. 我认为这是因为这不是传递参数的正确方法。

I've also tried calling it with the parameter names (UserID, ModeType) with and without the @, but that doesn't work. 我也尝试使用带有和不带有@的参数名称(UserID,ModeType)来调用它,但这不起作用。

What am I missing? 我想念什么?

Try with the below instead : 请尝试以下方法:

cs.setString(1, someUserID);
cs.setString(2, "0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM