java - 我的 SQL 准备语句通过某个 id 获取点的总和?

标签 java mysql sql

我正在尝试使用 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",

最佳答案

关于java - 我的 SQL 准备语句通过某个 id 获取点的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57634450/

相关文章:

java - Java中的类路径是什么?只与Java有关吗? C# 中有类路径吗?

java - 需要有关 Java 中的 SWT DateTime 的帮助

mysql - 在更新期间只为我的查询锁定表?

php - 从三个表中选择并显示结果

java - map 正在加载,但在 Android 开发中看不到 map

mysql - 诊断并避免 MySQL CPU 峰值

sql - 有没有办法以预定义的时间间隔运行存储过程?

python - SQL Server 临时表在 pyodbc 代码中不可用

sql - Access 查询 - 如果字段为空不输出?

java - 在 Intellij IDEA 中用 build.gradle 替换 Build Artifact