com.mysql.jdbc.MysqlDataTruncation: Data truncatio

2020-05-23 17:23发布

I am using Java JDBC with MySQL and I get this error:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: 
Data too long for column 'column_name'

How do I remedy this error?

7条回答
beautiful°
2楼-- · 2020-05-23 17:50

How to reproduce this error in mysql terminal:

mysql> create table penguins(val CHAR(2));
Query OK, 0 rows affected (0.08 sec)

mysql> insert into penguins values("AB");
Query OK, 1 row affected (0.00 sec)

mysql> insert into penguins values("ABC");
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> show warnings;
+---------+------+------------------------------------------+
| Level   | Code | Message                                  |
+---------+------+------------------------------------------+
| Warning | 1265 | Data truncated for column 'val' at row 1 |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)

This is the error you are getting, however in the MySQL terminal shows it as a warning and still inserts your row, just silently truncates it.

The MySQL JDBC is not as forgiving, and spits a hard error and will not insert the row.

2 Solutions

Either Increase the capacity of the datatype you are inserting into, or decrease the size of the content you are placing in there.

Legend to increase the size of your column:

If you are using   then use this
-----------------  --------------
smallint           int
int                long
TEXT               LONGTEXT
LONGTEXT           CLOB
CLOB               Store the content to disk, and save the filename in the db.
查看更多
登录 后发表回答