When I recently upgraded to MySQL 4.1.10 and Connector/J 3.1.8, I got the following Exception:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data truncated for column ‘productid’ at row 1
In most cases, the exception is raised in the following two situtations:
1. The auto increment field is incremented using ‘’.
2. The date column is updated using ‘’.
In order to recreate the problem, I created two tables, named “product” and “orders”. The former is used to test the auto increment field, the latter is used to test the date filed.
CREATE TABLE `product` (
`productid` int(10) NOT NULL auto_increment,
`productname` varchar(50) NOT NULL default ‘’,
`price` decimal(10,2) NOT NULL default ‘0.00′,
PRIMARY KEY (`productid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `orders` (
`orderno` int(10) NOT NULL default ‘0′,
`orderdate` date default NULL,
PRIMARY KEY (`orderno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The following is the result of my test run from the mysql command line interface.
mysql> insert into product values('’, ‘test’, 100.00);
Query OK, 1 row affected, 1 warning (0.36 sec)
mysql> show warnings;
+———+——+————————————————+
| Level | Code | Message |
+———+——+————————————————+
| Warning | 1265 | Data truncated for column ‘productid’ at row 1 |
+———+——+————————————————+
1 row in set (0.00 sec)
mysql> insert into product values(null, ‘test again’, 20.00);
Query OK, 1 row affected (0.05 sec)
mysql> insert into orders values(1, ‘’);
Query OK, 1 row affected, 1 warning (0.05 sec)
mysql> show warnings;
+———+——+————————————————+
| Level | Code | Message |
+———+——+————————————————+
| Warning | 1265 | Data truncated for column ‘orderdate’ at row 1 |
+———+——+————————————————+
1 row in set (0.00 sec)
mysql> insert into orders values(2, null);
Query OK, 1 row affected (0.05 sec)
As you can see, when I changed the insert statement from insert('’, … ) to insert(null, …), it didn’t generate a warning. Finally, I found that the problem was already mentioned in the Connector/J documentation:
Prior to MySQL-4.1, the server itself couldn’t report truncation of values. Starting with Connector/J 3.1.0, the JDBC driver will issue warnings or throw DataTruncation exceptions as is required by the JDBC specification unless the connection was configured not to do so by using the property “jdbcCompliantTruncation” and setting it to “false”.
jdbcCompliantTruncation — Should the driver throw java.sql.DataTruncation exceptions when data is truncated as is required by the JDBC specification when connected to a server that supports warnings(MySQL 4.1.0 and newer)?
When I set jdbcCompliantTruncation=false in my connection url something like this:
String connStr = “jdbc:mysql://localhost/test?user=root&password=secret&jdbcCompliantTruncation=false”;
It works well.
In addition, when updating a date field use ‘’, the date column is overwritten with 0000-00-00, Which will cause the following exception when we retrieve the data using rs.getDate(”orderdate”):
java.sql.SQLException: Value ‘0000-00-00′ can not be represented as java.sql.Date
This problem is also mentioned in the Connector/J documentation:
Datetimes with all-zero components (’0000-00-00 …’) - These values can not be represented reliably in Java. Connector/J 3.0.x always converted them to NULL when being read from a ResultSet.
Connector/J 3.1 throws an exception by default when these values are encountered as this is the most correct behavior according to the JDBC and SQL standards. This behavior can be modified using the ‘ zeroDateTimeBehavior ‘ configuration property. The allowable values are: ‘exception’ (the default), which throws a SQLException with a SQLState of ‘S1009′, ‘convertToNull’, which returns NULL instead of the date, and ’round’, which rounds the date to the nearest closest value which is ‘0001-01-01′.
I solved the problem by setting the ‘zeroDateTimeBehavior’ property to ‘convertToNull’ as follows:
String connStr = “jdbc:mysql://localhost/test?user=root&password=secret&zeroDateTimeBehavior=convertToNull”;