简体   繁体   English

无法在 SQL 服务器上使用 Java 和 JDBC 执行存储过程

[英]Unable to execute stored Procedure using Java and JDBC on SQL server

I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far.我今天一直在尝试通过 JDBC 执行 MS SQL 服务器存储过程,但到目前为止一直没有成功。 The stored procedure has 1 input and 1 output parameter.存储过程有 1 个输入和 1 个 output 参数。 With every combination I use when setting up the stored procedure call in code I get an error stating that the stored procedure couldn't be found.在代码中设置存储过程调用时,我使用的每种组合都会出现错误,指出无法找到存储过程。 I have provided the stored procedure I'm executing below (NOTE: this is vendor code, so I cannot change it).我在下面提供了我正在执行的存储过程(注意:这是供应商代码,所以我无法更改它)。

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO


ALTER PROC [dbo].[spWCoTaskIdGen] 
@OutIdentifier int OUTPUT
AS

BEGIN
DECLARE @HoldPolicyId int
DECLARE @PolicyId char(14)

IF NOT EXISTS
(
SELECT *
FROM UniqueIdentifierGen (UPDLOCK)
)
INSERT INTO UniqueIdentifierGen VALUES (0)

UPDATE UniqueIdentifierGen 
SET 
    CurIdentifier = CurIdentifier + 1

SELECT @OutIdentifier = 
    (SELECT CurIdentifier
    FROM UniqueIdentifierGen)
END

The code looks like:代码如下所示:

 CallableStatement statement = connection
            .prepareCall("{call dbo.spWCoTaskIdGen(?)}");
    statement.setInt(1, 0);
    ResultSet result = statement.executeQuery();

I get the following error: SEVERE: Could not find stored procedure 'dbo.spWCoTaskIdGen'.我收到以下错误:严重:找不到存储过程“dbo.spWCoTaskIdGen”。

I have also tried我也试过

    CallableStatement statement = connection
            .prepareCall("{? = call dbo.spWCoTaskIdGen(?)}");
    statement.registerOutParameter(1, java.sql.Types.INTEGER);
    statement.registerOutParameter(2, java.sql.Types.INTEGER);
    statement.executeQuery();

The above results in: SEVERE: Could not find stored procedure 'dbo.spWCoTaskIdGen'.以上结果:严重:找不到存储过程“dbo.spWCoTaskIdGen”。

I have also tried:我也试过:

    CallableStatement statement = connection
            .prepareCall("{? = call spWCoTaskIdGen(?)}");
    statement.registerOutParameter(1, java.sql.Types.INTEGER);
    statement.registerOutParameter(2, java.sql.Types.INTEGER);
    statement.executeQuery();

The code above resulted in the following error: Could not find stored procedure 'spWCoTaskIdGen'.上面的代码导致以下错误:找不到存储过程“spWCoTaskIdGen”。

Finally, I should also point out the following:最后,我还要指出以下几点:

  1. I have used the MS SQL Server Management Studio tool and have been able to successfully run the stored procedure.我使用了 MS SQL Server Management Studio 工具,并且已经能够成功运行存储过程。 The sql generated to execute the stored procedure is provided below:下面提供了为执行存储过程而生成的 sql:

     GO DECLARE @return_value int; DECLARE @OutIdentifier int; EXEC @return_value = [dbo].[spWCoTaskIdGen] @OutIdentifier = @OutIdentifier OUTPUT; SELECT @OutIdentifier [@OutIdentifier], @return_value [Return Value]; GO
  2. The code being executed runs with the same user id that was used in point #1 above.正在执行的代码使用上面第 1 点中使用的相同用户 ID 运行。

  3. In the code that creates the Connection object I log which database I'm connecting to and the code is connecting to the correct database.在创建连接 object 的代码中,我记录了我正在连接的数据库,并且代码正在连接到正确的数据库。

Any ideas?有任何想法吗?

Thank you very much in advance.非常感谢您提前。

Most likely one of... 最有可能...

  1. The credentials uses have no rights to run the code. 凭据使用无权运行代码。 You'd need a GRANT EXECUTE in the script above 您需要在上面的脚本中GRANT EXECUTE

  2. Wrong database. 错误的数据库。 For example, the stored proc was created in master but you are connected to "MyDB" 例如,存储的proc是在master中创建的,但是您已连接到“ MyDB”

Since it can't even find the procedure, I would first look to make sure that your user has execute privileges for that procedure. 由于它甚至找不到该过程,因此我首先要确保您的用户具有该过程的执行特权。 You could try executing the proc with the same user from a tool like Squirrel. 您可以尝试从同一个用户(如Squirrel)执行proc。

Try it without the dbo. 不使用dbo.尝试一下dbo. owner designation: 所有者名称:

CallableStatement statement = connection.prepareCall("? = spWCoTaskIdGen(?)");
statement.registerOutParameter(1, java.sql.Types.INTEGER);
statement.registerOutParameter(2, java.sql.Types.INTEGER);
statement.executeQuery();

Also, and this is a longshot, are you sure you're in the correct database within the database server? 另外,这是一个远景,您确定您在数据库服务器中的正确数据库中吗?

I had the same problem using SQLServer 2012 Express.我在使用 SQLServer 2012 Express 时遇到了同样的问题。 I solved it starting the "SQL Server Browser" service.我解决了它启动“SQL Server Browser”服务。

Have you tried changing your Java code to use "exec" instead of "call"? 您是否尝试过将Java代码更改为使用“ exec”而不是“ call”? That is: 那是:

CallableStatement statement = connection
                    .prepareCall("{exec dbo.spWCoTaskIdGen(?)}");

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

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