Java Spring JDBC模板问题

发布于 2024-11-04 13:54:35 字数 1728 浏览 1 评论 0原文

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 = ? AND weather.date BETWEEN now()::date AND (now() + '? days')::date",
                                        this.w_mapper, cityId, days);
}

错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [SELECT weather.id, cities.name, weather.date, weather.degree FROM weather JOIN cities ON weather.city_id = cities.id WHERE weather.city_id = ? AND weather.date BETWEEN now()::date AND (now() + '? days')::date]; The column index is out of range: 2, number of columns: 1.; nested exception is org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.

它适用于:

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 = ? AND weather.date = now()::date",
                                        this.w_mapper, cityId);
}

所以问题是当我使用两个时?在我的查询中标记。 我怎样才能让它与 2 一起工作?标记???

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 = ? AND weather.date BETWEEN now()::date AND (now() + '? days')::date",
                                        this.w_mapper, cityId, days);
}

error :

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [SELECT weather.id, cities.name, weather.date, weather.degree FROM weather JOIN cities ON weather.city_id = cities.id WHERE weather.city_id = ? AND weather.date BETWEEN now()::date AND (now() + '? days')::date]; The column index is out of range: 2, number of columns: 1.; nested exception is org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.

it works with :

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 = ? AND weather.date = now()::date",
                                        this.w_mapper, cityId);
}

so the problem is when im using two ? marks in my query.
how can i make it work to with 2 ? marks???

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

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

发布评论

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

评论(3

醉生梦死 2024-11-11 13:54:35

问题可能出在这部分:

'? days'

问号位于文字字符串内,因此 sql 解析器无法识别它。 您可以尝试使用字符串连接运算符重写它,尽管我不能 100% 确定在这种情况下这是有效的语法。

根据 postgres wiki 上的此页面 您应该能够简单地省略字符串“days”,因为添加日期和整数会被解释为添加指定的天数。

BETWEEN now()::date AND now()::date + ?

The problem is probably in this part:

'? days'

The question mark is inside a literal string and so it is not recognized by the sql parser. You could try to rewrite it using the string concatenation operator, although I'm not 100% sure that is valid syntax in this case.

According to this page on the postgres wiki you should be able to simply omit the string 'days', since adding a date and an integer is interpreted as adding the specified number of days.

BETWEEN now()::date AND now()::date + ?
自在安然 2024-11-11 13:54:35

将 SQL 部分重写

AND weather.date BETWEEN now()::date AND (now() + '? days')::date

AND weather.date BETWEEN now()::date AND ?

,并使用完整的 java.sql.Date 值而不是 days 设置它。

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, days);
Date endDate = new Date(calendar.getTimeInMillis());
// ...

(再次强调,它是 java.sql.Date,而不是 java.util.Date!)

Rewrite the SQL part

AND weather.date BETWEEN now()::date AND (now() + '? days')::date

as

AND weather.date BETWEEN now()::date AND ?

and set it with a fullworthy java.sql.Date value instead of days.

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, days);
Date endDate = new Date(calendar.getTimeInMillis());
// ...

(once again, it's java.sql.Date, not java.util.Date!)

老街孤人 2024-11-11 13:54:35

该错误表明您在第一个 sql 语句中只有 1 个参数(即?),但您传递了两个参数。 Spring 不知道如何处理第二个参数。

The error is saying that you only have 1 param (ie a ?) in the first sql statement, but you are passing in two args. Spring doesn't know what to do with the second arg.

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