You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot切换PostgreSQL后POST/PUT请求SQL语法错误排查

问题分析与解决

核心原因

MySQL支持字符串到日期的隐式类型转换,但PostgreSQL对类型一致性要求更严格——当你的tanggal字段定义为date类型,却传入字符串(character varying)时,PostgreSQL不会自动转换,直接抛出BadSqlGrammarException。

解决方案

针对你使用JDBC Repo的场景,提供三种可行方案:

1. SQL语句显式转换日期

在INSERT/UPDATE语句中,用PostgreSQL的TO_DATE()函数将字符串转为date类型,注意格式要和传入的字符串匹配(比如yyyy-MM-dd):

// POST操作示例
public int saveData(String tanggal, ...) {
    String sql = "INSERT INTO your_table (tanggal, col1, col2) VALUES (TO_DATE(?, 'yyyy-MM-dd'), ?, ?)";
    return jdbcTemplate.update(sql, tanggal, val1, val2);
}

// PUT操作示例
public int updateData(String tanggal, ..., Long id) {
    String sql = "UPDATE your_table SET tanggal = TO_DATE(?, 'yyyy-MM-dd'), col1 = ? WHERE id = ?";
    return jdbcTemplate.update(sql, tanggal, val1, id);
}

2. Java代码中转换为日期类型

将传入的字符串参数转为LocalDate(推荐)或java.sql.Date,直接作为参数传入JDBC模板,驱动会自动映射到PostgreSQL的date类型:

import java.time.LocalDate;

// POST操作示例
public int saveData(String tanggalStr, ...) {
    LocalDate tanggal = LocalDate.parse(tanggalStr); // 需确保字符串格式符合ISO-8601(如yyyy-MM-dd)
    String sql = "INSERT INTO your_table (tanggal, col1, col2) VALUES (?, ?, ?)";
    return jdbcTemplate.update(sql, tanggal, val1, val2);
}

如果字符串格式不是ISO-8601,可指定格式化器:

import java.time.format.DateTimeFormatter;

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate tanggal = LocalDate.parse(tanggalStr, formatter);

3. 调整实体类字段类型(如果用RequestBody接收参数)

如果你的API通过@RequestBody接收请求体,确保实体类中tanggal字段用LocalDate而非String,Spring会自动完成请求体字符串到LocalDate的转换:

public class YourRequestDto {
    private LocalDate tanggal;
    // 其他字段、getter、setter
}

之后在JDBC操作中直接使用该LocalDate字段即可。

额外检查

  • 确认数据库连接URL配置了正确时区,避免日期转换时的时区问题:
    spring.datasource.url=jdbc:postgresql://localhost:5432/your_db?serverTimezone=Asia/Jakarta
    
  • 验证PostgreSQL驱动版本是否与Spring Boot版本兼容,推荐使用官方维护的驱动:
    <!-- Maven依赖 -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    

内容的提问来源于stack exchange,提问作者Timothy Justin William

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 00:05:46