SimpleJdbcTemplate 的 Postgres 参数查询

发布于 2024-10-20 19:54:16 字数 3155 浏览 1 评论 0原文

我正在尝试使用 Springs SimpleJdbcTemplate 对 Postgre 数据库执行参数查询。我调用查询的类如下所示:

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;
    }
}

}

我正在使用此类测试查询:

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);
}

}

我不断收到以下错误:

    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

看起来我的参数未填充。 我以前没有使用过 Postgre,所以任何帮助将不胜感激。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-10-27 19:54:16

参数不在带引号的字符串内处理,所以我想您需要将整个字符串作为单个参数传递:

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

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() + ")");   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文