[postgis] ClassCastException with PostGis Example
Paul Ramsey
pramsey at refractions.net
Tue Jan 8 11:27:23 PST 2002
Some tests might help see where things are breaking:
- Print out obj.toString() before casting it.
- Retrieve the geometry column as a String and print that out too.
Silvio wrote:
>
> Hi,
>
> I have just installed PostGis with PostGreSql but the PostGis Example
> doesn't work.
>
> This is the example (http://postgis.refractions.net/docs/x441.html)
>
> import java.sql.*;
> import java.util.*;
> import java.lang.*;
> import org.postgis.*;
>
> public class JavaGIS {
> public static void main(String[] args)
> {
> java.sql.Connection conn;
> try
> {
> /*
> * Load the JDBC driver and establish a connection.
> */
> Class.forName("org.postgresql.Driver");
> String url = "jdbc:postgresql://localhost:5432/database";
> conn = DriverManager.getConnection(url, "postgres", "");
>
> /*
> * Add the geometry types to the connection. Note that you
> * must cast the connection to the pgsql-specific connection *
> implementation before calling the addDataType() method.
> */
>
> ((org.postgresql.Connection)conn).addDataType("geometry","org.postgis.PGgeom
> etry");
>
> ((org.postgresql.Connection)conn).addDataType("box3d","org.postgis.PGbox3d")
> ;
>
> /*
> * Create a statement and execute a select query.
> */
> Statement s = conn.createStatement();
> ResultSet r = s.executeQuery("select AsText(geom) as geom,id from
> geomtable");
> while( r.next() )
> {
> /*
> * Retrieve the geometry as an object then cast it to the geometry
> type.
> * Print things out.
> */
>
> PGgeometry geom = (PGgeometry)r.getObject(1);
> ********************CAST ERROR*****************
>
> int id = r.getInt(2);
> System.out.println("Row " + id + ":");
> System.out.println(geom.toString());
> }
> s.close();
> conn.close();
> }
> catch( Exception e )
> {
> e.printStackTrace();
> }
> }
> }
>
> The compiling phase is ok
> After, when I try to run the class I get the following error:
>
> java.lang.ClassCastException: java.lang.String
> at PostGis.main(PostGis.java:42)
>
> (the error is referred to the line 42 where I have written "CAST ERROR")
>
> I hope that somebody can help me!!
>
> Thanks in advance.
>
> Silvio B.
>
>
> To unsubscribe from this group, send an email to:
> postgis-unsubscribe at yahoogroups.com
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
--
__
/
| Paul Ramsey
| Refractions Research
| Email: pramsey at refractions.net
| Phone: (250) 885-0632
\_
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Tiny Wireless Camera under $80!
Order Now! FREE VCR Commander!
Click Here - Only 1 Day Left!
http://us.click.yahoo.com/WoOlbB/7.PDAA/ySSFAA/PhFolB/TM
---------------------------------------------------------------------~->
To unsubscribe from this group, send an email to:
postgis-unsubscribe at yahoogroups.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the postgis-users
mailing list