Calling stored procedure with named JDBC parameters raises exception

后端 未结 2 2086
攒了一身酷
攒了一身酷 2021-01-15 01:04
package com.brookfieldres.operations;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import jav         


        
相关标签:
2条回答
  • 2021-01-15 01:52

    This may not be exactly what you were hoping for, but it does work for passing parameters to the stored procedure by name, thereby allowing us to

    • specify the parameters in an arbitrary order in the command text, and
    • omit parameters that have default values.

    For the stored procedure

    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
    

    the JDBC call

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

    returns

    Hello, Gord!
    

    (Tested with Microsoft JDBC Driver 4.1 for SQL Server.)

    0 讨论(0)
  • 2021-01-15 01:52

    Use index based set methods.

               _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");
    
    0 讨论(0)
提交回复
热议问题