如何使用JDBC从SQL查询结果中获取消息?

时间:2019-05-07 14:24:43

标签: java sql-server tsql jdbc ssms

我正在使用JDBC执行sql服务器事务查询。我想做的是获取受查询中每个命令行影响的行数并打印出来。我想获得的信息类似于我们可以在sql-server的消息窗格中看到的消息。在这种情况下: https://ibb.co/1zTKXDz

我尝试使用函数getWarnings(),但是它只显示我想要的第一行(“ 1”,受TRANSACTION内的第一个操作影响的行数),而第二行显示其值为空,我不明白为什么。

// Create and execute an SQL statement that returns some data.
                String SQL = "DECLARE @old nvarchar(13)\r\n" + 
                        "SET @old = 'UR0000325F001'\r\n" + 
                        "\r\n" + 
                        "BEGIN transaction MyT1\r\n" + 

                        "       UPDATE BERA7.dbo.MG89_LIFOART     SET MG89_CODART_MG66 = @old WHERE MG89_CODART_MG66 =@old\r\n" + 
                        "       UPDATE VIBO7.dbo.MG89_LIFOART     SET MG89_CODART_MG66 = @old WHERE MG89_CODART_MG66 =@old\r\n" + 
                        "       UPDATE VITM7.dbo.MG89_LIFOART     SET MG89_CODART_MG66 = @old WHERE MG89_CODART_MG66 =@old\r\n" + 
                        "       PRINT '--- MG1Q_ARTNOMENCLATURE ---'\r\n" + 
                        "       DELETE from BERA7.dbo.MG1Q_ARTNOMENCLATURE  WHERE MG1Q_CODART_MG66 =@old\r\n" + 
                        "       DELETE from VIBO7.dbo.MG1Q_ARTNOMENCLATURE     WHERE MG1Q_CODART_MG66 =@old\r\n" + 
                        "       DELETE from VITM7.dbo.MG1Q_ARTNOMENCLATURE     WHERE MG1Q_CODART_MG66 =@old\r\n" + 
                        "       PRINT '--- MG43_ARTPERS ---'\r\n" + 
                        "       DELETE from BERA7.dbo.MG43_ARTPERS      where MG43_CODART_MG66 =@old\r\n" + 
                        "       DELETE from VIBO7.dbo.MG43_ARTPERS             where MG43_CODART_MG66 =@old\r\n" + 
                        "       DELETE from VITM7.dbo.MG43_ARTPERS             where MG43_CODART_MG66 =@old\r\n" + 
                        "       PRINT '--- PD27_ANAFORMULE ---'\r\n" + 
                        "       DELETE FROM BERA7.dbo.PD27_ANAFORMULE\r\n" + 
                        "COMMIT transaction MyT1";

                stmt = con.createStatement();
                System.out.println(stmt.executeUpdate(SQL));

                SQLWarning warning = stmt.getWarnings();

                while (warning != null)
                {
                   warning = warning.getNextWarning();
                   System.out.println(warning);
                }
        }

    // Handle any errors that may have occurred.
    catch (Exception e) {
        e.printStackTrace();
        e.getMessage();
    }

    finally {
        if (rs != null) try { rs.close(); } catch(Exception e) {}
            if (stmt != null) try { stmt.close(); } catch(Exception e) {}
            if (con != null) try { con.close(); } catch(Exception e) {}
    }

我期望的是每个命令受影响的行数。

0 个答案:

没有答案