如何从Java调用存储过程

时间:2015-08-24 14:51:26

标签: java sql-server tsql stored-procedures

我在SQL Server数据库中有一个存储过程。此存储过程将所有记录返回到临时表中。

这是我的存储过程的最后一部分:

SELECT 
    CONVERT(varchar, [Calcolato_TimespanInizio], 103) as DataAccesso,
    CAST(DATEPART(HOUR, [Calcolato_TimespanInizio]) AS NVARCHAR(100))+':'+CAST(DATEPART(MINUTE, [Calcolato_TimespanInizio]) AS NVARCHAR(100)) as OraInizio,
    CAST(DATEPART(HOUR, [Calcolato_TimespanFine]) AS NVARCHAR(100))+':'+CAST(DATEPART(MINUTE, [Calcolato_TimespanFine]) AS NVARCHAR(100)) as OraFine,
    T.IDNostro as CodiceCategoriaOperatore,
    1 as numQta,
    '' as descrizioniNote,
    DATEPART(weekday,[Calcolato_TimespanInizio]) as flagFestivoPrefestivo,
    '' as CodiceErogatore,
    COALESCE(Dichiarati_KmPercorsi, Calcolati_KmPercorsi) as numKmPercorsi,
    CodiceVoucher as identificativoPianoAssistenziale,
    ID as identificativoIntervento,
    '' as incaricoAmbulatoriale,
    '' as incaricoContinuitaAssistenziale,
    @ID_PRESTAZIONE AS ElencoPrestazioni
FROM 
    AA_V_Accessi A
JOIN 
    AA_V_Operatori O ON A.CodiceOperatore = O.CODICE
JOIN 
    AA_V_T_EXP_T_TipologieOperatori T ON T.IDNostro = O.IDTipologiaOperatore
                                      AND T.ASL = 'br'

RETURN 1

现在我想从客户端java调用这个存储过程。

这是代码:

            String query = "{call [dbo].[_WS_ElencoAccessi]}";

            CallableStatement stmt = db.prepareCall(query);
            stmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(3, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(4, java.sql.Types.INTEGER);
            stmt.registerOutParameter(5, java.sql.Types.INTEGER);
            stmt.registerOutParameter(6, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(7, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(8, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(9, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(10, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(11, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(12, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(13, java.sql.Types.VARCHAR);
            stmt.registerOutParameter(14, java.sql.Types.VARCHAR);

            ResultSet rs = stmt.executeQuery();   // Ottengo il ResultSet dell'esecuzione della query
            while(rs.next()) {   
                Accessi accesso = new Accessi();
                accesso.setDataAccesso(rs.getString(1));

                lista.add(accesso);
            }

但是我得到了一个SQLServerException

  

(com.microsoft.sqlserver.jdbc.SQLServerException)com.microsoft.sqlserver.jdbc.SQLServerException:L'索引1超出了允许的范围。

1 个答案:

答案 0 :(得分:0)

在存储过程名称后添加括号,并使用等量的问号来匹配您希望填写的参数数量。

例如

{call MyProc(?,?,?)}