code: private String getErrorCode(SQLException ex) {
          String result = null;
          SQLException nestedEx = null;
  
          if (ex.getErrorCode() != 0) {
              result = Integer.toString(ex.getErrorCode());
          }
  
          if (result == null) {
              nestedEx = ex.getNextException();
              if (nestedEx != null) {
                  result = Integer.toString(nestedEx.getErrorCode());
              }
          }
  
          return result;
      }
docs: Reads SQLException hierarchy and returns error code
  
  @param ex top SQL exception
  @return error code as String