code: private String getSqlState(SQLException ex) {
          String result = ex.getSQLState();
          SQLException nestedEx = null;
  
          if (result == null) {
              nestedEx = ex.getNextException();
              if (nestedEx != null) {
                  result = nestedEx.getSQLState();
              }
          }
  
          return result;
      }
docs: Reads SQLException hierarchy and returns SQL state.
  
  @param ex top SQL exception
  @return sql state