java is not catching ms sql stored procedure raise error

泪湿孤枕 提交于 2019-12-22 08:24:10

问题


I have a stored procedure in my SQL Server 2008 database and I am devloping a java application that use a sqljdbc4 connection. Everything works fine, even procedure call, but there is one thing - in some case java does not catch raised exception throwen by the procedures and specified when there is call like a select or update (delete insert)

here is an exemple

AS
BEGIN
    DECLARE @c INT
    SELECT @c= COUNT(nomUtilisateur) 
        FROM Utilisateur2
    PRINT @c
    RAISERROR(N'error message personalise',18,1)
END

in this case java catche the exception

try{
    CallableStatement cll=con.prepareCall("EXEC f3 ");
    cll.execute();

    System.out.println("fin EXEC f3 ? ");}
catch (SQLException e) {
    System.out.println("sqlerror state: "+e.getSQLState() +" |error code: "
        + e.getErrorCode()+" |message: "+e.getMessage());
    e.printStackTrace();
} 

but when I use a select like this


ALTER PROCEDURE [dbo].[f3]
AS
BEGIN
    declare @c INT
    SELECT @c= COUNT(nomUtilisateur) 
        FROM Utilisateur2
    select * from Utilisateur2
    PRINT @c
    RAISERROR(N'error message personalise',18,1)
END

or an update, java continues excution with out catching the exception. Its like it doesn't wait for the end of the procedure.

So my question is, am I using the stored procedure incorrectly or is it a bug in jdbc driver or it prefer to use an out parameter and leave this strategy; i Googled this probleme without finding anything.

Personally, I want to use the raised error


回答1:


Try removing PRINT statement and put "SET NOCOUNT ON" in proc's code, it might be that driver gets confused.



来源:https://stackoverflow.com/questions/12989302/java-is-not-catching-ms-sql-stored-procedure-raise-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!