import java.sql.*;
import java.io.FileInputStream;
import java.util.Properties;
class JdbcSelectObjCast {
  public static void main (String args []) throws Exception {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Properties props = new Properties();
    props.load(new FileInputStream("conn.props"));
    Connection conn = DriverManager.getConnection(props.getProperty("url"), props);
    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery("SELECT * FROM " + args[0]);
    ResultSetMetaData rsmd = rset.getMetaData();
    int numCols = rsmd.getColumnCount();

    // continue next slide


    while (rset.next()) {
     for(int i=1; i<=numCols; i++) {
      Object value = rset.getObject(i);
      // System.out.println(rsmd.getColumnType(i));
      if (rset.wasNull()) System.out.print("null");
      else if (rsmd.getColumnType(i) == 2008 &&
         ((java.sql.SQLData)value).getSQLTypeName() == "MYGEOCOORD")  // <<<< test
      { GeoCoordJ gc = (GeoCoordJ)value;        // <<<< cast
        System.out.print(gc + " ");
        System.out.print(gc.distance(new GeoCoordJ(0,0)));  // <<<< call
      }
      else if (rsmd.getColumnType(i) == 2008 && value instanceof GeoCoordJ) // <<<< test
      { GeoCoordJ gc = (GeoCoordJ)value;        // <<<< cast
        System.out.print(gc + " ");
        System.out.print(gc.distance(new GeoCoordJ(0,0))); // <<<< call
      }
      else if (rsmd.getColumnType(i) == java.sql.Types.STRUCT)
      { System.out.print(((java.sql.Struct)value).getSQLTypeName() + "( ");
        Object[] attrs = ((java.sql.Struct)value).getAttributes();
        for (int j = 0; j < attrs.length; j++) System.out.print(attrs[j] + " ");
        System.out.print(")");
      }
      else System.out.print(value + "  "); // also covers Java instances (uses toString())
    }
    System.out.println();  }
  rset.close(); stmt.close(); conn.close();  }}
