[postgis-users] NewBee, connect to a PostgisDatabase via ODBC and C++

Malm Paul paul.malm at saabgroup.com
Tue Oct 9 02:08:45 PDT 2007


Hi,
This is perhaps a trivial issue, but I cant find any examples of
connecting to a postgis db via a C++ application and ODBC.
The example below is take from the PostGis manual and is an example of
what I would like to do.
When I installed PostgreSQL and PostGIS I entered the following
parameters:

service name= PostgreSQL Database server 8.2
username=postgres
userDomain= WEB-TEST
pwd = postgre
Port:5432
Coding UTF8
Owner = postgres_owner
Owner pwd = pgpwd


Postgis database:
DatabaseName= fmadb
port: 5432
username = postgres_owner
password = pgpwd

Kind regards,
Paul




/*************************************************************
/*** This is what I would like to do in C++ with ODBC instead
/*************************************************************
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.PG
geometry");
 -
((org.postgresql.Connection)conn).addDataType("box3d","org.postgis.PGbox
3d");
/*
* 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);
int id = r.getInt(2);
System.out.println("Row " + id + ":");
System.out.println(geom.toString());
}
s.close();
conn.close();
}
catch( Exception e )
{
e.printStackTrace();
}



More information about the postgis-users mailing list