使用命名的JDBC参数调用存储过程会引发异常

时间:2016-01-27 19:23:27

标签: java sql-server jdbc

USE [NewLocationDB]
GO

/****** Object:  StoredProcedure [dbo].[iCurrentLocations01]    Script Date:     01/27/2016 1:27:10 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




ALTER PROCEDURE [dbo].[iCurrentLocations01]
@RunDate datetime, 
@CompanyId varchar, 
@LocationId varchar, 
@rlpOpenDate varchar 

AS 
DECLARE @RetVal int 
SET @RetVal = 0 

INSERT CurrentLocations (RunDate, CompanyId, LocationId, rlpOpenDate )
VALUES (@RunDate, @CompanyId, @LocationId, @rlpOpenDate)

SET @RetVal = @@ERROR
Return @RetVal

GO

因此,当我尝试运行我的线束时,会建立连接。但是,当程序试图调用我的存储过程" iCurrentLocations01"。它在我的日志中返回错误消息

  

" 2016-01-27 13:11:17错误SQLConnection:97 - 参数RunDate是   没有为存储过程iCurrentLocations01定义。"

有人可以解释我在这里做错了什么吗?程序btw从LDAP目录获取信息并将其插入本地数据库。

修改 - 这是我的存储过程:

$access_token ='nlauth_account=XXXXXXX, nlauth_email=admin@example.com, nlauth_signature=password';
$header = array('Content-Type: application/json','Authorization : NLAuth '.$access_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER,$header) ;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

curl_close($ch);
$result = json_decode($result, true);
echo '<pre>';print_r($result);exit;

2 个答案:

答案 0 :(得分:2)

这可能不是您所希望的,但它确实可以通过名称将参数传递给存储过程,从而允许我们

  • 在命令文本中以任意顺序指定参数,
  • 省略具有默认值的参数。

对于存储过程

CREATE PROCEDURE [dbo].[my_sp] 
    @p1 nvarchar(10) = N'Hello', 
    @p2 nvarchar(10) = N'world'
AS
BEGIN
    SET NOCOUNT ON;
    SELECT @p1 + N', ' + @p2 + N'!' AS response;
END

JDBC调用

try (CallableStatement s = conn.prepareCall("{CALL my_sp (@p2=?)}")) {
    s.setString(1, "Gord");
    try (ResultSet rs = s.executeQuery()) {
        rs.next();
        System.out.println(rs.getString("response"));
    }
}

返回

Hello, Gord!

(使用Microsoft JDBC Driver 4.1 for SQL Server测试。)

答案 1 :(得分:1)

使用基于索引的集合方法。

           _cs = getConnection().prepareCall("{call iCurrentLocations01(?, ?, ?, ?)}");
            _cs.setTimestamp(1, RunDate);
            _cs.setString(2, rlpCompanyid);
            _cs.setString(3, rlpLocationid);
            _cs.setString(4, rlpOpenDate );
            returnVal = _cs.executeUpdate(); 
            System.out.println("2");