从JDBC,Java调用Sqlserver存储过程

时间:2015-04-17 14:23:34

标签: java jdbc

我可以从toad sql server运行此查询。

exec msp_FormBsBa_yeni 0,'20150101','20150131',5000,0,2,0,null,1,null,0

从我在java中的应用程序,它生成错误,因为该语句没有返回结果集。我尝试了很多东西,但我无法解决它。谢谢你的帮助。

    // Connection,Resultset ve PrepStatement Declaration
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rsa = null;
    List<EntCari> listrows = new ArrayList<EntCari>();

    try {
        connection = ConnectionFactory.getConnection();

        CallableStatement callableStatement = connection
                .prepareCall("{call msp_FormBsBa_yeni(0,N'20150101',N'20150131',5000,0,2,0,null,1,null,0)}");

        rsa = callableStatement.executeQuery();

        // 2:resultset check
        if (!rsa.next()) {
            System.out.println("no data");
        } else {
            do {
                // System.out.println("data exists");
                // Statements
                EntCari row = new EntCari();
                row.setMusteriadi(rsa.getString("Unvan"));// rsa.getxxx("column")
                listrows.add(row);

            } while (rsa.next());
        }

        // 2---

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        DbUtil.close(rsa);
        DbUtil.close(pstmt);
        DbUtil.close(connection);
    }
来自toad sql server的

结果如下所示。

Unvan | Ulkesi | VergiKimlikNo | TCKimlikNo | BelgeSayisi | Toplam | CariKod

客户a | 052 | 19697583840 | 1 | 2323,00 | HT00084

客户b | 052 | | 2 | 2111,00 | HT01022

存储过程如下(sp的一部分,很长)

/****** Object: Procedure [dbo].[msp_FormBsBa_yeni]   Script Date: 18.04.2015 13:43:44 ******/
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
CREATE PROCEDURE dbo.msp_FormBsBa_yeni
@vFirmaNo    as integer,
@IlkTarih    as datetime,
@SonTarih    as datetime,
@MinTutar    as float,
@Bs_Ba_tip   as bit,
@BirlestimeTuru as tinyint,
@SonradanMuhasebelesenSeriDahilEdilmesin_fl as bit,
@SonradanMuhasebelesenSeriStr nvarchar(MAX),
@Aylik_BsBa_fl as bit,
@PerakendeCariKodu AS nvarchar(25),
@EvrakDetayliRapor_fl as bit
AS
BEGIN  /*dbo.fn_GetByteParam(1048)=1 ÖTV stok maliyetine eklensinmi*/
Declare @otv_vergino as tinyint
set @otv_vergino = dbo.fn_GetByteParam(855) /*ÖTV vergi tipi*/
Declare @otv_kdv_orani as FLOAT
set @otv_kdv_orani = 0.0
if @otv_vergino between 1 and 10
set @otv_kdv_orani = dbo.fn_VergiYuzde(@otv_vergino)
Declare @kontrol_belge_tarihinden as integer
set @kontrol_belge_tarihinden = dbo.fn_GetByteParam(4173) /*BsBa_Kontrol_Belge_tarihinden_fl*/
if exists (select * from tempdb..sysobjects where name LIKE '#BsBaEvrakDetayliTablo%') Drop Table dbo.#BsBaEvrakDetayliTablo
select
TABLONO,
MIN(CHRECNO) AS CHRECNO,
CARI,
MAX(TARIH) AS TARIH,
TIP,
SERI,
SIRA,
MAX(CINS)AS CINS,
MAX(BELNO)AS BELNO,
MAX(BELTAR)AS BELTAR...

这是错误信息

com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:394)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:283) 

3 个答案:

答案 0 :(得分:2)

您无法在返回游标/ executeQuery()的Transact-SQL CallableStatement上调用ResultSet。调用该过程的正确方法是:

// You should really use bind variables here...
CallableStatement call = connection
    .prepareCall("{call msp_FormBsBa_yeni(?, ?, ?, ...)}");
call.setInt(1, ...);
call.setString(2, ...);

// This returns true if there are any remaining ResultSets to fetch
if (call.execute()) {
    do {
        try (ResultSet rs = call.getResultSet()) {
            // Consume the result set here...
        }
    }

    // Move on to the next result set, if any
    while (call.getMoreResults());
}

如果您想要完全通用,那么您还必须检查Statement.getUpdateCount(),以防更新计数与您的过程中的结果集交织。

答案 1 :(得分:1)

当我在数据库客户端(例如SQL Server Management Studio)中执行查询时,将返回一个表。

我的存储过程很长,有一些插入和更新语句。如果存储过程在最终选择之前执行任何插入或更新,则JDBC(SQL Server驱动程序)与行计数混淆(并且语句未返回结果集的异常将被抛出)。幸运的是,修复非常简单:我只需要在存储过程开始时添加SET NOCOUNT ON。所以我的代码看起来像:

CREATE PROCEDURE dbo.msp_FormBsBa_yeni
@vFirmaNo as integer,
@IlkTarih as datetime,
@SonTarih as datetime,
@MinTutar as float,
@Bs_Ba_tip as bit,
@BirlestimeTuru as tinyint,
@SonradanMuhasebelesenSeriDahilEdilmesin_fl as bit,
@SonradanMuhasebelesenSeriStr nvarchar(MAX),
@Aylik_BsBa_fl as bit,
@PerakendeCariKodu AS nvarchar(25),
@EvrakDetayliRapor_fl as bit
AS
BEGIN
  SET NOCOUNT ON

我非常感谢你的贡献

答案 2 :(得分:0)

以下是实现这一目标的两种方法..

String SPsql = "EXEC msp_FormBsBa_yeni ?,?,?";   // for stored proc taking 3 params
Connection con = SmartPoolFactory.getConnection();   
PreparedStatement ps = con.prepareStatement(SPsql);
ps.setEscapeProcessing(true);
ps.setQueryTimeout(<timeout value>);
ps.setString(1, "val");
ps.setString(2, "val";
ps.setString(3, "val");
ResultSet rs = ps.executeQuery();

或使用JDBC scape语法

CallableStatement cStmt = conn.prepareCall("{call msp_FormBsBa_yeni (?, ?, ?) }");
cStmt.setString(1, "val");
cStmt.setString(2, "val");
cStmt.setString(3, "val");
cStmt.registerOutParameter(4, Types.INTEGER); // register the output params 
cStmt.registerOutParameter("inOutParam", Types.INTEGER); // or by param name

有关详情,请查看thisthis

我希望这可以提供帮助。