Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to execute a parameter query for a Postgre database using Springs SimpleJdbcTemplate. My class that calls the query looks like this:

public class GeoCodeServiceImpl extends SimpleJdbcDaoSupport implements GeoCodeServiceInterface {
public static final String SELECT_STATEMENT = "SELECT ste_code, ste_code_type, name, fips_code " +
                                              "FROM \"steGeo\" " +
                                              "WHERE st_contains( the_geom, ST_GeomFromText('POINT(:lon :lat)',4269))";
public List<GeoCode> getGeoResults(Double lon, Double lat) throws DataAccessException {
    MapSqlParameterSource mappedParms = new MapSqlParameterSource("lon", lon.toString());
    mappedParms.addValue("lat", lat.toString());
    SqlParameterSource namedParms = mappedParms;
    List<GeoCode> resultList = getSimpleJdbcTemplate().query(SELECT_STATEMENT, new GeoCodeRowMapper(), namedParms);
    if (resultList == null || resultList.size() == 0) {
        logger.warn("No record found in GeoCode lookup.");
    return resultList;
protected static final class GeoCodeRowMapper implements RowMapper<GeoCode> {
    public GeoCode mapRow(ResultSet rs, int i) throws SQLException {
        GeoCode gc = new GeoCode();
            gc.setCode(rs.getString(1));
            gc.setType(rs.getString(2));
            gc.setFips(rs.getString(3));
            gc.setName(rs.getString(4));
        return gc;

I am testing the query with this class:

public class GeoCodeServiceTest {
public static void main(String[] args) {
    Double lat = 40.77599;
    Double lon = -83.82322;
    String[] cntxs = {"project-datasource-test.xml","locationService-context.xml"};
    ApplicationContext ctx = new ClassPathXmlApplicationContext(cntxs);
    GeoCodeServiceImpl impl = ctx.getBean("geoCodeService", GeoCodeServiceImpl.class);
    List<GeoCode> geoCodes = impl.getGeoResults(lon, lat);
    System.out.println(geoCodes);

I keep getting the following error:

    2011-03-07 08:16:29,227 [main] DEBUG org.springframework.jdbc.support.SQLErrorCodesFactory - SQL error codes for 'PostgreSQL' found
2011-03-07 08:16:29,227 [main] DEBUG org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator - Unable to translate SQLException with SQL state 'XX000', error code '0, will now try the fallback translator
2011-03-07 08:16:29,227 [main] DEBUG org.springframework.jdbc.support.SQLStateSQLExceptionTranslator - Extracted SQL state class 'XX' from value 'XX000'
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [SELECT ste_code, ste_code_type, name, fips_code FROM "steGeo" WHERE st_contains( the_geom, ST_GeomFromText('POINT(:lon :lat)',4269))]; SQL state [XX000]; error code [0]; ERROR: parse error - invalid geometry
  Hint: "POINT(" <-- parse error at position 6 within geometry; nested exception is org.postgresql.util.PSQLException: ERROR: parse error - invalid geometry
  Hint: "POINT(" <-- parse error at position 6 within geometry

It looks like my parameters are not populated. I haven't used Postgre before so any help would be much appreciated.

Thanks

Parameters are not handled inside quoted strings, so I guess you need to pass the whole string as a single parameter:

public static final String SELECT_STATEMENT = 
    "SELECT ste_code, ste_code_type, name, fips_code " +
    "FROM \"steGeo\" " + 
    "WHERE st_contains( the_geom, ST_GeomFromText(:pt, 4269))"; 
MapSqlParameterSource mappedParms = new MapSqlParameterSource("pt", 
    "POINT(" + lon.toString() + " " + lat.toString() + ")");   
                I had a feeling it was something like that.  Thank you for taking the time to look at it.
– blong824
                Mar 7, 2011 at 15:47
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.