# java.sql.DataTruncation: 列'X'上的数据截断

- **ID:** `java/sql-data-truncation`
- **领域:** java
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

DataTruncation 发生在 JDBC 驱动程序尝试插入或更新超过数据库模式中列定义长度或精度的值时。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| MySQL Connector/J 8.0 | active | — | — |
| PostgreSQL JDBC 42.5 | active | — | — |
| H2 2.1 | active | — | — |

## 解决方案

1. ```
   Validate and truncate the data in Java code before insertion. For example: if (value.length() > columnMaxLength) { value = value.substring(0, columnMaxLength); }
   ```
2. ```
   Increase the column size in the database schema using ALTER TABLE: ALTER TABLE my_table MODIFY COLUMN my_column VARCHAR(500); and ensure application logic respects the new limit.
   ```

## 无效尝试

- **** — Increases database storage and may allow invalid data, but truncation can still occur if the new limit is again exceeded. (40% 失败率)
- **** — Does not address the actual data size mismatch; the truncation still happens at the database level. (60% 失败率)
