SQL server exception : when using jdbc connection to retrieve data
I use callable
interface of Java and try to read the output variable value of a stored procedure written in SQL server.
Stored procedure:
CREATE PROCEDURE [Get_Project_Name_Test]
@project_id int,
@project_name varchar(150) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
开发者_C百科SET @project_name = (SELECT Name from Project where Project_Id = @project_id)
END
GO
Here @project_id
is input variable and @project_name
is output variable. I want to read this value of the output variable from Java application. When I run the code I get a sql exception.
It says:
The formal parameter "@project_id" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output.
Actually, this is not an output parameter. @project_id
is the input parameter used to retrieve the result.
Java code which used to call this stored procedure
CallableStatement cstmt = con.prepareCall("{call Get_Project_Name_Test(?)}");
cstmt.setInt(1, 148);
cstmt.registerOutParameter(1, java.sql.Types.VARCHAR);
cstmt.executeQuery();
String x = cstmt.getString(1);
System.out.println(x);
How can I figure out the cause of the error?
Should
cstmt.registerOutParameter(1, java.sql.Types.VARCHAR);
be
cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
?
精彩评论