Java – Preparedstatement does not read all parameters of PostGIS geography
I have the following JDBC code Please note that I am trying to use PostGIS geographic location:
PreparedStatement stmt = db.prepareStatement("INSERT INTO " + "source_imagery (image_path,boundary,image_time)" + " VALUES (?,ST_GeographyFromText('POLYGON((" + "? ?,? ?,? ?))'),?)"); stmt.setString(1,file.getAbsolutePath()); stmt.setDouble(2,bounds.getY()); stmt.setDouble(3,bounds.getX()); ...
I get the following exception in the last line of code:
org.postgresql.util.PsqlException: The column index is out of range: 3,number of columns: 2.
As far as I know, it thinks I have only 2 parameters, but you can see that I intend to have 10 there I don't know why it doesn't read any parameters in polygon I know this SQL statement is valid if I use it directly in the database, but I don't know what I need to change to make it work in my java code Any ideas?
Solution
Your question is:
'POLYGON((? ?,? ?))'
Is an SQL string text containing exactly eight question marks Since this is an SQL string literal, any question mark in it will not be regarded as a placeholder This leaves two placeholders: the first placeholder in the values list and the last placeholder
You must build polygons in other ways There may be more than St_ Geographiyfromtext is a better method, but, alas, I don't know what it is. I haven't set up PostGIS anywhere If necessary, you can manually build polygon strings using standard strings, and then use placeholders:
VALUES (?,ST_GeographyFromText(?),?)
ST_ The placeholder in geographiyfromtext will be treated as a placeholder because it is not in string text, you can use stmt Setstring assigns a value to it