When I run the sql statement below through psql it works fine, but when I try to run the same query by building it with a preparedstatement, it fails.
INSERT INTO Hvbp
(provider_number, weighted_clinical_process,
weighted_patience_experience, total_performance_score,
coordinates, latitude, longitude, address, city, state, zip)
VALUES
('010092', 43.909090909091, 13.5, 57.409090909091,
'POINT(33.206201 -87.525480)', 33.206200613000476,
-87.52548020899968, '809 UNIVERSITY BOULEVARD EAST', 'TUSCALOOSA', 'AL', '');
The error I keep getting is
org.postgresql.util.PSQLException: ERROR: column "coordinates" is of type geography but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 203
The coordinates column is of type GEOGRAPHY(POINT)
I know this is an old problem, but I just spend most of the day debugging the same basic problem and finally found a fix
What you're trying to do, is provide the
POINTusing WKT and have the server automatically convert that into aGeometry.And as you've found that works if you include the WKT inside the body of the SQL, but fails if you use a parameter on a prepared statement.
There are 3 options for fixing it:
Use st_GeographyFromText in your SQL like so:
And then set your parameters as WKT with
setString.Depending on the frameworks involved, that might not be possible for you.
Use
setObjecton the preparedStatement instead ofsetString. For example:Change your JDBC driver settings to send strings as
unspecifiedtype, and then the server will do type conversions for you. To do that you want to change your JDBC URL to something like