import java.sql.*;

public class JGeoCoord implements java.sql.SQLData {
 private double lat, lon;

  public String getSQLTypeName() {
    return "MYGEOCOORD";   // just to illustrate something
 }
 public void readSQL(SQLInput stream, String typeName)
   throws SQLException {
     lat = stream.readDouble();
     lon = stream.readDouble();
 }
 public void writeSQL(SQLOutput stream)
   throws SQLException {
     stream.writeDouble(lat);
     stream.writeDouble(lon);
 }
 // ... continue next slide






  // the constructor is not needed in the database
  // but we need it for "outside" with JDBC
  public JGeoCoord(double la, double lo) {
     lat = la;  lon = lo;
  }
  // then, the standard constructor must be explicitly
  // defined because it is needed in the database
  public JGeoCoord() {
     lat = 0;  lon = 0;
  }
  // used when printing e.g. in JDBC
  public String toString() {
    return "JJGeoCoord(" + lat + "/" + lon + ")";
  }
  public double distance(JGeoCoord other) {
    return
      6370 *  Math.acos(
         Math.cos(this.lat/180*3.14) *
         Math.cos(other.lat/180*3.14) *
         Math.cos( (this.lon - other.lon)
                   /180*3.14 ) +
         Math.sin(this.lat/180*3.14) *
         Math.sin(other.lat/180*3.14)  );
} }
