Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Point (Geometry) value in PostgreSQL with JDBC

Please give me code example to insert records containing SQL statement:

insert into TABLE 
(id, value1, value2, point, value3) 
values
(1,'A', 'M', POINT (13.45646, 56.61782),5); 

in JDBC/Postgresql code.

If anyone has solution of PreparedStatement or anyother useful solution it is highly welcomed!

like image 562
Adnan Ali Avatar asked May 17 '12 23:05

Adnan Ali


People also ask

Does JDBC work with PostgreSQL?

The PostgreSQL JDBC Driver allows Java programs to connect to a PostgreSQL database using standard, database independent Java code. pgJDBC is an open source JDBC driver written in Pure Java (Type 4), and communicates in the PostgreSQL native network protocol.

What is JDBC URL for PostgreSQL?

Here is the documentation for JDBC, the general URL is "jdbc:postgresql://host:port/database"

What is the difference between geometry and geography data in PostGIS?

PostGIS gives you a choice of two different ways to store geospatial data: Geometry, where it assumes all of your data lives on a Cartesian plane (like a map projection); Geography, where it assumes that your data is made up of points on the earth's surface, as specified by latitudes and longitudes.


1 Answers

At the simplest level, you can build a prepared statement using geometry constructors to pass parameters.

insert into "TABLE"(id, value1, value2, point, value3)
values(1, $1, $2, ST_SetSRID(ST_MakePoint($3, $4), 4326)), $5);

Where $3 and $4 are the longitude and latitude.


See also the PostGIS documentation for the JDBC interface which may be useful with other geometry types (LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon).

like image 61
Mike T Avatar answered Nov 15 '22 13:11

Mike T