0

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

1 Answer 1

1

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() + ")");   
Sign up to request clarification or add additional context in comments.

1 Comment

I had a feeling it was something like that. Thank you for taking the time to look at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.