# java.sql.DataTruncation: Data truncation on column 'X'

- **ID:** `java/sql-data-truncation`
- **Domain:** java
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

DataTruncation occurs when a JDBC driver attempts to insert or update a value that exceeds the column's defined length or precision in the database schema.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| MySQL Connector/J 8.0 | active | — | — |
| PostgreSQL JDBC 42.5 | active | — | — |
| H2 2.1 | active | — | — |

## Workarounds

1. **Validate and truncate the data in Java code before insertion. For example: if (value.length() > columnMaxLength) { value = value.substring(0, columnMaxLength); }** (85% success)
   ```
   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.** (90% success)
   ```
   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.
   ```

## Dead Ends

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