在BeanPropertyRowMapper中传递的参数中的PostgreSQL语法错误

时间:2015-07-28 08:46:59

标签: java spring postgresql spring-jdbc

我想在我的代码中创建一个动态String,并使用BeanPropertyRowMapper将其添加到预先存在的查询中。这是创建字符串的函数:

nested exception is org.postgresql.util.PSQLException: ERROR: syntax error       at or near "$3"
Posizione: 3939
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:722)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:772)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:192)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:199)
at it.ads.etas.bi.dashboard.service.impl.jdbc.CemKpiServiceJDBC.loadGrid(CemKpiServiceJDBC.java:263)
at it.ads.etas.bi.dashboard.view.cem.CEMCoverage.init(CEMCoverage.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
... 60 more
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$3"
Posizione: 3939
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:703)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
... 74 more

我想将我在函数中创建的字符串放在:filters placeholder中。 在查询中我有2个:sideLenght占位符和一个:过滤器,当我执行代码时出现此错误:

BeanPropertyRowMapper

我将主SQL查询字符串params和jdbcTemplate.query传递给 SELECT mnc, network_type, (geom) as geom, ROUND(AVG(signal_strength)) AS signalStrength, COUNT(DISTINCT (_id)) AS count_network_event FROM ( SELECT _id, networkstates.mnc, data, (CASE WHEN network_type = 'EDGE' OR network_type = 'GPRS' OR network_type = 'GSM' THEN '2G' WHEN network_type = 'HSPA+' OR network_type = 'HSPA' OR network_type = 'HSDPA' OR network_type = 'UMTS' OR network_type = 'WCDMA' THEN '3G' WHEN network_type = 'LTE' THEN '4G' ELSE 'UNKNOWN' END) as network_type, comune, ST_GeomFromGeoHash(ST_GeoHash(ST_POINT(longitude, latitude),:sideLenght),:sideLenght) AS geom, signal_strength FROM ( SELECT locations.date, latitude, longitude, device.imei, device.mnc, comuni_italiani.comune FROM locations INNER JOIN device ON device.imei = locations.imei AND locations.date BETWEEN device.date AND device.last_date INNER JOIN comuni_italiani ON comuni_italiani._id = locations.id_comuni_italiani ) AS locations LEFT JOIN ( SELECT _id, device.imei, device.mnc, network_states.date, TO_TIMESTAMP(network_states.network_time/1000) AT TIME ZONE 'cest' AS data, network_type, signal_strength FROM network_states INNER JOIN device ON network_states.imei = device.imei AND network_states.network_time BETWEEN device.date AND device.last_date WHERE signal_strength IS NOT NULL ) AS networkstates ON networkstates.date BETWEEN locations.date-5000 AND locations.date AND networkStates.imei = locations.imei AND networkstates.mnc = locations.mnc WHERE networkstates._id IS NOT NULL ) AS prima :filters GROUP BY geom, mnc, network_Type--, comune, data 方法。 这是带占位符的查询:

WHERE (network_type = 'net1' OR network_type = 'net2' OR network_type = 'net3') AND (mnc = 'mnc1' OR mnc = 'mnc2' OR mnc = 'mnc3' OR mnc = 'mnc4')

我要添加的字符串就是这样的字符:
    {{1}}
有人可以解释一下错误在哪里吗?

1 个答案:

答案 0 :(得分:0)

我认为问题在于您使用命名参数而不是字符串连接 尝试拆分您的查询:

query = "..... ) AS prima" + filters;

而不是

query = "..... ) AS prima :filters";