English 中文(简体)
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. The stored procedure has 1 input and 1 output parameter. 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 .

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 .

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 .

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. The sql generated to execute the stored procedure is provided below:

    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.

  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.

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

  2. Wrong database. For example, the stored proc was created in master but you are connected to "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.

Try it without the 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. I solved it starting the "SQL Server Browser" service.

Have you tried changing your Java code to use "exec" instead of "call"? That is:

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签