[postgis-users] ClassCastException accesing feature geometries from postgresql
Venu Kanaparthy
madhavsinghk at yahoo.com
Wed Sep 24 18:37:33 PDT 2003
Hi all, I am trying to read feature geometries from posgresql over jdbc in a JSP page.
I have a parcel tables with multipolygons. I am getting classcastexception
at following statement,
PGgeometry the_geom = (PGgeometry)recordSet.getObject(2);
I want to read the point, line and polygon geometries and create a GML file.
one more question i have is, PGgeometry class is part of classes that came with
postgresql.jar file. There is one more jar file (postgis.jar), which containts POSTGIS
supported geomtries. How can read geometries from postgresql type into postgis?
Is it possible ? Any help will be great.
I am including the source code for jsp file: dbconnect.jsp
<%@ page import="java.sql.*,java.util.*,java.io.*,org.postgis.*,org.postgresql.util.*,org.postgis.MultiPolygon,org.postgis.Geometry,java.lang.*" %>
<html> <body>
<% try { Class.forName("org.postgresql.Driver").newInstance(); %>
<h1><font color="blue">Driver loaded !!!! </h1>
<% } catch (ClassNotFoundException ex)
{ System.out.println("Error loading the JDBC driver: " + ex);
%> <h1><font color="red">Error loading the JDBC driver</font></h1>
<% return;
}
String url = "jdbc:postgresql://localhost:5432/test";
String host = "localhost"; String database = "test";
int port = 3306; // String URL = "jdbc:mysql:" + host + ":" + port + ":" + database;
// String URL ="jdbc:mysql://localhost:3306/test";
String username = ""; String password = ""; Connection connection = null;
try{
connection = DriverManager.getConnection(url,"vkanaparthy", "");
// connection = DriverManager.getConnection(URL, username, password);
System.out.println("\nMade Connection OK"); %>
<h1><font color="blue">Database Connection Successfull !!!! </h1>
<%
// connection.close(); // connection = null;
} catch (java.sql.SQLException ex)
{ %>
<h1><font color="red">Dabase Connection Unsuccesful !!!!! + <% ex.printStackTrace(new PrintWriter(out)); %></font></h1>
<% System.err.println("Error getting the connection: " + ex); return;
}
try {
// ((org.postgresql.PGConnection)connection).addDataType("geometry","org.postgis.PGgeometry");
// ((org.postgresql.PGConnection)connection).addDataType("box3d","org.postgis.PGbox3d");
((org.postgresql.PGConnection)connection).addDataType ("geometry","org.postgis.PGgeometry");
((org.postgresql.PGConnection)connection).addDataType ("multipolygon","org.postgis.MultiPolygon");
((org.postgresql.PGConnection)connection).addDataType ("box3d","org.postgis.PGbox3d");
((org.postgresql.PGConnection)connection).addDataType ("polygon","org.postgis.Polygon");
((org.postgresql.PGConnection)connection).addDataType ("point","org.postgis.Point");
Statement s = connection.createStatement();
// ResultSet r = s.executeQuery("select gid, AsText(the_geom) as geom, type, material from sewerlines");
ResultSet r = s.executeQuery("select gid, AsText(the_geom) as geom from parcel");
while( r.next() ) {
// String sgeom = (String)r.getString(1);
PGgeometry the_geom = (PGgeometry)r.getObject(2);
// PGgeometry the_geom = new PGgeometry(sgeom);
int id = r.getInt(1); // String type = r.getString(3); // String mat = r.getString(4); %>
<table border=1> <tr><td width=70>
<% // out.println("Row " + geom + " : "+ type + ":" + mat + "\n"); out.println("\n"); %>
</td></tr> </table>
<% //out.println(geom.toString());
} connection.close(); } catch(Exception ex1) { %> <% ex1.printStackTrace(new PrintWriter(out)); %>
<% } %> <h1> </h1></body> </html>
Exception:
java.lang.ClassCastException at org.apache.jsp.dbconnect_jsp._jspService(dbconnect_jsp.java:133) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256) at
---------------------------------
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20030924/e18a9ccc/attachment.html>
More information about the postgis-users
mailing list