Re: Prepared statement with function as argument: how to bind values? - Mailing list pgsql-jdbc

From alexbruy
Subject Re: Prepared statement with function as argument: how to bind values?
Date
Msg-id 26b8239e-b98b-4edf-ad07-28dd9063ddd7@m37g2000yqc.googlegroups.com
Whole thread Raw
In response to Prepared statement with function as argument: how to bind values?  (alexbruy <alexander.bruy@gmail.com>)
Responses Re: Prepared statement with function as argument: how to bind values?
Re: Prepared statement with function as argument: how to bind values?
List pgsql-jdbc
On 23 сен, 22:25, msakre...@truviso.com (Maciek Sakrejda) wrote:
> > As I
> > understand, it says about number of columns because there are 5
> > columns listed in query and I try to bind value to non existing 6
> > column.
>
> As I understand it,
>
>  (1) the parameter handling code doesn't know or care you're executing
> an insert, so it shouldn't deal in "columns" here, just in parameters
>  (2) the question marks in 'POINT(?,?)' should not be treated as
> parameter markers at all: it should just be a String constant.
>
> Can you show us exactly how you're binding parameters in jdbc?

Here is code

String sql = "INSERT INTO
poi(geom,latitude,longitude,description,comment) VALUES
(ST_GeomFromText('POINT(? ?)', 4326), ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);

stmt.setFloat(1, lon);
stmt.setFloat(2, lat);
stmt.setFloat(3, lon);
stmt.setFloat(4, lat);
stmt.setTimestamp(5, descr);
stmt.setString(6, comment);

Currently I solve this problem with creating SQL in runtime from
several string pieces like this

String sql = "INSERT INTO
poi(geom,latitude,longitude,description,comment) VALUES
(ST_GeomFromText('POINT(";
sql += Float.toString(lon) + " ";
sql += Float.toString(lat) + ")', 4326),";
....

Bye,
Alex

pgsql-jdbc by date:

Previous
From: "Kevin Grittner"
Date:
Subject: Re: Patch for binary receive of basic array types
Next
From: Mikko Tiihonen
Date:
Subject: Patch for allowing explicit enable/disable of binary transfer for each OID type