简体   繁体   English

用于SimpleJdbcTemplate的Postgres参数查询

[英]Postgres Parameter Query for SimpleJdbcTemplate

I am trying to execute a parameter query for a Postgre database using Springs SimpleJdbcTemplate. 我正在尝试使用Springs SimpleJdbcTemplate对Postgre数据库执行参数查询。 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. 我以前没有使用过Postgre,所以任何帮助将不胜感激。

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() + ")");   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM