Java Spring JDBC template problem -


public list<weather> getweather(int cityid, int days) {     logger.info("days: " + days);     return getsimplejdbctemplate().query("select weather.id, cities.name, weather.date, weather.degree " +                                         "from weather join cities on weather.city_id = cities.id " +                                         "where weather.city_id = ? , weather.date between now()::date , (now() + '? days')::date",                                         this.w_mapper, cityid, days); } 

error :

org.springframework.web.util.nestedservletexception: request processing failed; nested exception org.springframework.dao.dataintegrityviolationexception: preparedstatementcallback; sql [select weather.id, cities.name, weather.date, weather.degree weather join cities on weather.city_id = cities.id weather.city_id = ? , weather.date between now()::date , (now() + '? days')::date]; column index out of range: 2, number of columns: 1.; nested exception org.postgresql.util.psqlexception: column index out of range: 2, number of columns: 1. 

it works :

public list<weather> getweather(int cityid, int days) {     logger.info("days: " + days);     return getsimplejdbctemplate().query("select weather.id, cities.name, weather.date, weather.degree " +                                         "from weather join cities on weather.city_id = cities.id " +                                         "where weather.city_id = ? , weather.date = now()::date",                                         this.w_mapper, cityid); } 

so problem when im using 2 ? marks in query. how can make work 2 ? marks???

the problem in part:

'? days' 

the question mark inside literal string , not recognized sql parser. you try rewrite using string concatenation operator, although i'm not 100% sure valid syntax in case.

according this page on postgres wiki should able omit string 'days', since adding date , integer interpreted adding specified number of days.

between now()::date , now()::date + ? 

Comments