code: public static double getDistanceBetween( IHMConnection connection, Coordinate p1, Coordinate p2, int srid ) throws Exception {
          if (srid < 0) {
              srid = 4326;
          }
          GeometryFactory gf = new GeometryFactory();
          LineString lineString = gf.createLineString(new Coordinate[]{p1, p2});
          String sql = "select GeodesicLength(LineFromText(\"" + lineString.toText() + "\"," + srid + "));";
          try (IHMStatement stmt = connection.createStatement(); IHMResultSet rs = stmt.executeQuery(sql);) {
              if (rs.next()) {
                  double length = rs.getDouble(1);
                  return length;
              }
              throw new RuntimeException("Could not calculate distance.");
          }
  
      }
docs: Calculates the GeodesicLength between to points.
  
  @param connection the database connection.
  @param p1 the first point.
  @param p2 the second point.
  @param srid the srid. If <0, 4326 will be used. This needs to be a geographic prj.
  @return the distance.
  @throws Exception