我正在尝试使用 JDBC 和准备好的语句从我的 MySQL 数据库中获取总点数。我目前有
private static final String SELECT_TOTAL_POINTS_FROM_LEVEL_BY_CUSTOMER =
"select sum(points) from level_up where customer_id = ?";
@Override
public int getTotalLevelUpPointsByCustomerId(int customerId) {
int points = jdbcTemplate.queryForObject(SELECT_TOTAL_POINTS_FROM_LEVEL_BY_CUSTOMER,
Integer.class);
return points;
}
我的 Controller 方法是
@GetMapping("/points/{customerid}")
ResponseStatus(HttpStatus.OK)
public int getTotalPoints(@PathVariable("customerid") int customerId) {
return service.getTotalPoints(customerId);
}
我怎样才能只用客户 ID 从数据库中取回积分?
错误如下
"message": "StatementCallback; bad SQL grammar [select sum(points) from level_up where customer_id = ?]; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1",
最佳答案
您使用的方法不允许传递参数。使用允许传入参数的方法,例如:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html#queryForObject-java.lang.String-java.lang.Object:A-int:A-java.lang.Class-
关于java - 我的 SQL 准备语句通过某个 id 获取点的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57634450/