gpt4 book ai didi

java - SQL服务器异常: when using jdbc connection to retrieve data

转载 作者:行者123 更新时间:2023-12-02 08:13:16 25 4
gpt4 key购买 nike

我使用Java的callable接口(interface)并尝试读取在SQL Server中编写的存储过程的输出变量值。

存储过程:

CREATE PROCEDURE [Get_Project_Name_Test] 
@project_id int,
@project_name varchar(150) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET @project_name = (SELECT Name from Project where Project_Id = @project_id)
END
GO

这里@project_id是输入变量,@project_name是输出变量。我想从 Java 应用程序读取输出变量的值。当我运行代码时,出现 sql 异常。

它说:

The formal parameter "@project_id" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output.

实际上,这不是一个输出参数。 @project_id 是用于检索结果的输入参数。

用于调用此存储过程的Java代码

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);

如何找出错误原因?

最佳答案

应该

cstmt.registerOutParameter(1, java.sql.Types.VARCHAR);

 cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);

关于java - SQL服务器异常: when using jdbc connection to retrieve data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6939703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com