Search code examples
javasql-serverdb2mainframe

DB2OLEDB Driver and error messages in SQLException from SQL Server 2016


We are using SQL Server 2016 with a linked server to zDB2 (mainframe) via Microsoft OLE DB Provider for DB2 Version 5.0.

When executing a failing SQL statement remote on a linked server (zDB2) the error description is clear in SQL studio but useless in Java.

Example of statement (missing schema name):

EXEC (N'UPDATE TABLENAME SET COLUMN1=''SOMEVALUE'' WHERE COLUMN2=  ''032'' ') AT ZDB2 

Gives the following result in SQL Studio:

OLE DB provider "DB2OLEDB" for linked server "ZDB2" returned message "DB2GRP.TABLENAME IS AN UNDEFINED NAME SQLSTATE: 42704, SQLCODE: -204". 
Msg 7215, Level 17, State 1, Line 6 
Could not execute statement on remote server 'ZDB2'. 

But the following result in a Java SQLException:

com.microsoft.sqlserver.jdbc.SQLServerException: Could not execute statement on remote server 'ZDB2'. 
SQL State: S0001 
SQL Error code: 7215 

Using a debugger and inspecting the SQL Exception object, it seems the SQLException has no other data on the error.

How can I get a more useful explanation out of the SQLException on what went wrong besides "Could not execute statement on remote server" ?

And where is the documentation of what S0001 and 7215 means ? (I already googled)

Thank you in advance.


Solution

  • Apparently errors from a linked server are stored as warnings on the statement rather than part of the SQLException when using Microsofts JDBC Driver.

    This is how i did it:

    try{
        ...
    } catch (SQLException e) {
        SQLWarning sqlWarning = currentStatement.getWarnings();
        if (sqlWarning != null) {
            warning = sqlWarning.getMessage();
        }
    }